Android TextView.getLayout() returns null - android

I get null in Layout object which I try to obtain from a Textview each time after configuration change.
The method containing the code is called from onResume().
I suspect I need to do some additional setup for it.
I have studied similar questions, but there isn't a clear answer to it.
What is the event which signals that Layout is ready after configuration changes?
Is there some documentation for it?
EDITED:
public class BookDisplayAct extends Activity implements View.OnClickListener {
...
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textViewMain);
textView.setPadding(15, 15, 15, 15);
...
}
#Override
protected void onResume() {
...
renderBookView();
}
public void renderBookView() {
...
justifyText(spannable, textView);
...
}
private Spannable justifyText(Spannable spannable, TextView textView) {
Layout layout = textView.getLayout();
int line_count = layout.getLineCount();
...
}
I get the NullPointer exception with the last line.

Just change your justifyText method as below
private Spannable justifyText(Spannable spannable, TextView textView) {
ViewTreeObserver observer= textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Layout layout = textView.getLayout();
int line_count = layout.getLineCount();
}
});
...
}

Related

Why I'm getting null exceptionj when I run the same function for the second time?

I have a button over which I call the fucntion click
WHen I click the button 2nd time, app crashes.
It is because I call setEdit on null object.
Is there some other way to solve the problem than the second variation
given below with if(!=null) ? Is there an explanation why I'm getting this null exception ?
public void click(View view) {
EditText editText = (EditText)findViewById(R.id.simpleEditText);
if (editText != null) { editText.setId(202); }
}
But his works fine:
public void click(View view) {
EditText editText = (EditText)findViewById(R.id.simpleEditText);
if (editText != null) { editText.setId(202); }
}
THis is from the conversation, showing him how to get views and store them.
public class MyActivity extends Activity {
private TextView text1;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.mylayout);
text1 = findViewById(R.id.text);
}
private void someFunc() {
text1.setText("Hey I used the variable");
}
}

Storing Seekbar value but keep getting that I have to use final

"Variable is accessed within inner class Needs to be declared final" is the the error I get at first. So I change it to final. Once I change it to final I get a different error saying "Cannot assign value to final variable". I'm kind of stuck here on what to do here. Its giving me the error on int exam_grade
public class CalculateGradeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seekbar();
}
public void seekbar(){
SeekBar seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
final TextView text_view = (TextView) findViewById(R.id.percentageSeekbar1);
int exam_grade;
TextView text_view5 = (TextView) findViewById(R.id.numeric_grade_id_output);
text_view5.setText(String.valueOf(exam_grade));
int progress = seek_bar1.getProgress();
text_view.setText(String.valueOf(progress));
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
exam_grade = progress;
text_view.setText(String.valueOf(progress) + "%");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}//ends seekbar function
}
Hi don't declare your textview as final, put it in the class attributes instead.
public class CalculateGradeActivity extends Activity {
TextView text_view;
SeekBar seek_bar1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
text_view = (TextView) findViewById(R.id.percentageSeekbar1);
seekbar();
}
Do the same for all views(Declare them as attributes).
final means it can only be assigned once.
I feel sure that textview shouldn't be final.
anyway,
try putting the declarations of exam grade and TextView in your onCreate...
setContentView(R.layout.calculate_grade_view);
int exam_grade;
TextView text_view = (TextView) findViewById(R.id.percentageSeekbar1);
seekbar();

How to update a TextView from within onPostExecute

Need help here, about onPostExecute. If I want to put the update on textView what should be the code and what should I do?.
#Override
protected Value[] doInBackground(Integer... params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);
Value[] variableValues = batteryLevel.getValues();
return variableValues;
}
#Override
protected void onPostExecute(Value[] variableValues) {
// Update your views here
}
Do as follows, save reference to TextView as member and use it in nested AsyncTask:
public class YourActivity extends Activity{
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
textView = (TextView)findViewById(R.id.yourTextViewID);
...
}
private YourAsyncTask extends AsyncTask{
...
#Override
protected void onPostExecute(Value[] variableValues) {
if(textView != null)
textView.setText("your text");
}
}
}
If you want get last value from Ubidots to your textview. You can try this, but u must set the textview first OnCreate
#Override
protected void onPostExecute(Value[] variableValues) {
// Update your views here
String status = Double.toString(variableValues[0].getValue());
//this is textview for show last value from ubidots
mBatteryStatus.setText(status);
}
}

Making ImageView invisible from method

I have created HideImages() function as shown below. The problem is, that running this code causes NullPointerExcpection. When I comment out the setVisibility lines, it works fine. What am I doing wrong?
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
ImageView img_w0 = (ImageView)findViewById(R.id.img0);
ImageView img_w1 = (ImageView)findViewById(R.id.img1);
ImageView img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
Make all the references of ImageView as Global as
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
I think this may case the problem because you had already initialize ImageView above the onCreate() method the why you declare here,
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
The problems is here. You're already declared the ImageView objects as globally. And, again you're declaring internally in onCreate()
So, just remove the declaration inside of onCreate() and run. Like below -
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
}

Assigning Values to a TextView from a different class

TestaActivity.java
public class TestaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.textView1);
tvText.setText("Sample");
}
}
Print.java
public class Print {
public Print(Context tempContext) {
//I want to assign the value to the tvText from here
}
}
In the above example, as you can see I have set the text in tvText to "Sample". In the same way, I need to assign the textView1 ID with some value inside Print class, once it is created.
Please help me to figure out the way to do it.
If your class Print is instantiated when TestaActivity is on the screen, then you can get tvText reference, passing to Print in some way a TestaActivity reference.
Maybe you could pass it via constructor:
From TestaActivity you do:
Print print = new Print(this);
where this represents the instance of TestaActivity.
And then in your Print code you can do:
TextView tvText = (TextView)((TestaActivity)context.findViewById(R.id.textView1));
tvText.setText("Sample");
Another solution is provide an interface from TestaActivity, transparent for the outside, which manage your changes on the textview (or whatever).
Something like that:
private TextView tvText;
public void setTvText(String str){
tvText.setText( str );
}
And then in your Print class:
((TestaActivity)context).setTvText( "Sample" );
try as:
public class Print {
protected TestaActivity context;
public Print(Context tempContext) {
context = tempContext;
}
public void changetextViewtext(final String msg){
context.runOnUiThread(new Runnable() {
#Override
public void run() {
//assign the value to the tvText from here
context.tvText.setText("Hello Test");
}
});
}
}
and call changetextViewtext from Activity for Changeing TextView Text from Print Class
public class TestaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.textView1);
tvText.setText("Sample");
Print myPrint = new Print(this);
myPrint.changetextViewtext("Hello World !!!");
}
}
as your need!!!!:)
#imran - the solution is correct except that you would want to pass the TextView as an argument in the constructor or the method.
Harcoding TextView in a method is bad because you cannot be reuse it.

Categories

Resources