我在我的项目中使用ViewPager,并且在设置同一布局上的不同数据时遇到了一些问题。所以我需要在我的textViews上使用setText来定制每个页面。 但我不能在包含该TextView的ViewGroup充气后在TextView中设置TextText()。 下面的代码:如果删除setText()
线setText()膨胀后
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);
}
}
一切工作好了。这里的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);
你还可以向你展示布局文件,你有这些textviews。 – Vikram 2012-02-15 15:33:00
Question为什么要创建TextView [] text = new TextView [7];并循环通胀?我没有看到循环内的第二个循环的意义...... – 2012-02-15 15:34:16
b'cos我稍后需要该循环。我刚刚得到了问题所在。我没有在我的主布局(R.layout.pages)这些textViews,但我有他们在我的其他布局,我膨胀(R.layout.schedule)。所以我现在需要的是如何引用不在我的布局中的TextView,而是在我膨胀的布局中。 – 2012-02-15 15:43:06