I’m using a ViewPager in my project, and having a little problem at setting different data on the same layout. So I need to use a setText on my textViews to customize each page.
But I can’t setText() in TextView after inflating ViewGroup that includes that TextView.
Here’s the code:
public class ScheduleActivity extends Activity {
private List<View> mPages;
private ViewPager mPager;
private TitlePageIndicator mTitleIndicator;
private ArrayList<String> titles;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pages);
initUi();
}
private void initUi() {
LayoutInflater inflater = LayoutInflater.from(this);
mPages = new ArrayList<View>();
titles = new ArrayList<String>();
titles.add(getString(R.string.monday));
titles.add(getString(R.string.tuesday));
titles.add(getString(R.string.wednesday));
titles.add(getString(R.string.thursday));
titles.add(getString(R.string.friday));
titles.add(getString(R.string.saturday));
for (String title : titles) {
TextView[] text = new TextView[7];
text[0] = (TextView) findViewById(R.id.text1);
text[1] = (TextView) findViewById(R.id.text2);
text[2] = (TextView) findViewById(R.id.text3);
text[3] = (TextView) findViewById(R.id.text4);
text[4] = (TextView) findViewById(R.id.text5);
text[5] = (TextView) findViewById(R.id.text6);
text[6] = (TextView) findViewById(R.id.text7);
for (int i = 0; i < 7; i++) {
text[i].setText(R.string.monday);
}
View day = inflater.inflate(R.layout.schedule, null);
day.setTag(title);
mPages.add(day);
}
MainPageAdapter adapter = new MainPageAdapter(mPages);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(adapter);
mPager.setCurrentItem(0);
mTitleIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
mTitleIndicator.setViewPager(mPager);
mTitleIndicator.setCurrentItem(0);
final float density = getResources().getDisplayMetrics().density;
mTitleIndicator.setTextSize(15 * density); // 15dp
mTitleIndicator.setFooterLineHeight(1 * density); // 1dp
mTitleIndicator.setFooterIndicatorHeight(3 * density); // 3dp
mTitleIndicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
mTitleIndicator.setFooterColor(0x34B4E3);
mTitleIndicator.setTextColor(0xAA000000);
mTitleIndicator.setSelectedColor(0xFF000000);
mTitleIndicator.setSelectedBold(true);
}
}
Everything works just great if I remove the setText() line. Here’s the LogCat:
02-15 18:17:54.294: D/AndroidRuntime(465): Shutting down VM
02-15 18:17:54.323: W/dalvikvm(465): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-15 18:17:54.353: E/AndroidRuntime(465): FATAL EXCEPTION: main
02-15 18:17:54.353: E/AndroidRuntime(465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.stiggpwnz.schedule/com.android.stiggpwnz.schedule.ScheduleActivity}: java.lang.NullPointerException
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.os.Looper.loop(Looper.java:123)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 18:17:54.353: E/AndroidRuntime(465): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 18:17:54.353: E/AndroidRuntime(465): at java.lang.reflect.Method.invoke(Method.java:521)
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 18:17:54.353: E/AndroidRuntime(465): at dalvik.system.NativeStart.main(Native Method)
02-15 18:17:54.353: E/AndroidRuntime(465): Caused by: java.lang.NullPointerException
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.stiggpwnz.schedule.ScheduleActivity.initUi(ScheduleActivity.java:63)
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.stiggpwnz.schedule.ScheduleActivity.onCreate(ScheduleActivity.java:36)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-15 18:17:54.353: E/AndroidRuntime(465): ... 11 more
Line 63 is setText() line.
UPD: changed it to look like this, but still the same exception:
day = inflater.inflate(R.layout.schedule, null);
day.findViewById(R.layout.schedule);
for (String title : titles) {
TextView[] text = new TextView[7];
text[0] = (TextView) findViewById(R.id.text1);
text[1] = (TextView) findViewById(R.id.text2);
text[2] = (TextView) findViewById(R.id.text3);
text[3] = (TextView) findViewById(R.id.text4);
text[4] = (TextView) findViewById(R.id.text5);
text[5] = (TextView) findViewById(R.id.text6);
text[6] = (TextView) findViewById(R.id.text7);
for (int i = 0; i < 7; i++) {
text[i].setText(R.string.monday);
}
day.setTag(title);
mPages.add(day);
}
UPD 2: this is how it started to work:
View day = inflater.inflate(R.layout.schedule, null); TextView tv = (TextView)day.findViewById(R.id.text1);
the problem is here:
You haven’t yet inflated the layout, and therefore when you write
findview will return null, because there are no views to search for!
you must first inflate the layout and only AFTER you can programmatically change its content.