Reputation: 23
I have this code, does anybody know why I get an error when I compile this on my Phone?
public void onSectionAttached(int number) {
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
textView.setVisibility(View.GONE);
break;
case 3:
mTitle = getString(R.string.title_section3);
textView.setVisibility(View.GONE);
break;
}
}
EDIT : This is the error I get, it goes wrong on the setVisibitily lines.
Upvotes: 1
Views: 160
Reputation: 5867
Next time please attach the relevant logs to your question. It makes things so much simpler..
Anyway the only causes for error I can see in your code are:
A. textView.setVisibility() is called from a thread other than the UI thread. If that is the case, do something like to solve your problem:
myActivity.runOnUiThread(new Runnable() {
public void run() {
onSectionAttached(num);
}
});
B. You have not called setContentView() to your layout prior to activating this code.
C. Your layout does not contain a TextView element called textView1. In which case findViewById() wil return null and textView.setVisibility() will result in NPE.
Upvotes: 2