我在我的项目中使用ViewPager,并且在同一布局上设置不同数据时遇到一些问题。所以我需要在textViews上使用setText来自定义每个页面。 但是在对包含TextView的ViewGroup进行膨胀之后,我无法在TextView中设置text()。 这是代码:
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);
}
}
如果删除setText()
行,一切都很有效。这是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
第63行是setText()行。
UPD:改变它看起来像这样,但仍然是同样的例外:
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:这就是它开始工作的方式:
View day = inflater.inflate(R.layout.schedule, null); TextView tv = (TextView)day.findViewById(R.id.text1);
答案 0 :(得分:1)
如果在调用TextView setText时遇到NullPointerException,则需要检查布局文件以确保TextView上有正确的ID。 findViewById返回一个空引用,这意味着你传递的ID在使用的布局中不存在(R.layout.pages)
答案 1 :(得分:1)
问题在于:
private void initUi() {
LayoutInflater inflater = LayoutInflater.from(this);
你还没有夸大布局,因此当你写
时text[0] = (TextView) findViewById(R.id.text1);
findview将返回null,因为没有要搜索的视图!
您必须首先对布局进行充气,并且只有在您可以通过编程方式更改其内容之后。