我有一个带ViewPager的FragmentActivity。在ViewPager里面总共有3个不同的视图。
用户选择包含信息的行。选择后,FragmentActivity将加载并在ViewPager中的视图中显示数据。
我希望预先加载数据,因此当活动最终出现时,所有内容都在正确的小部件中。我选择在FragmentActivity的onCreate方法中执行此操作。但是,当我尝试访问ViewPager中视图的任何小部件时,我得到一个NullPointerException。
我不知道怎么解决这个问题。
按要求:
这段代码处理调用FragmentActivity - “co”是一个包含将要显示的信息的序列化对象。
TimesheetEdit ts = new TimesheetEdit();
Intent result = new Intent(this, ts.getClass());
result.putExtra("ClientObject", this.co);
if (v != null) {
result.putExtra("key", v.getId());
}
startActivityForResult(
result,
viewType == getResources().getInteger(R.integer.TIMESHEET) ? getResources()
.getInteger(R.integer.TIMESHEET) : getResources()
.getInteger(R.integer.EXPENSES));
下一组代码是Fragmentactivity的onCreate方法。最后的Log.i仅用于测试目的,但这也是我的NullPointerException发生的地方。我假设因为某些原因,尚未从ViewPager中正确获取View。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timesheetdb_displays_timesheet_edit);
Log.i("info", "in timesheet edit onCreate");
//getActionBar().setDisplayHomeAsUpEnabled(true); //Requires API 11 or
// higher
this.co = (ClientObject) getIntent().getExtras().get("ClientObject");
if (getIntent().getExtras().get("key") != null) {
this.key = (Integer) getIntent().getExtras().get("key");
this.co.setRowKey(this.key);
} else {
key = 0;
}
this.vp = (ViewPager) findViewById(R.id.timePager);
this.vp.setOffscreenPageLimit(2);
this.vp.setAdapter(new ViewPagerAdapter());
this.vp.setCurrentItem(0);
View v = vp.getChildAt(0);
Log.i("info", String.format("View id in Hex is: %X", v.getId()));
//initComponents();
}
最后,对应于此错误的LogCat是:
11-27 13:21:57.857: W/dalvikvm(710): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-27 13:21:57.917: E/AndroidRuntime(710): FATAL EXCEPTION: main
11-27 13:21:57.917: E/AndroidRuntime(710): java.lang.RuntimeException: Unable to start activity ComponentInfo{timesheetdb.Login/timesheetdb.Displays.TimesheetEdit}: java.lang.NullPointerException
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.os.Looper.loop(Looper.java:137)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-27 13:21:57.917: E/AndroidRuntime(710): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 13:21:57.917: E/AndroidRuntime(710): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 13:21:57.917: E/AndroidRuntime(710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-27 13:21:57.917: E/AndroidRuntime(710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-27 13:21:57.917: E/AndroidRuntime(710): at dalvik.system.NativeStart.main(Native Method)
11-27 13:21:57.917: E/AndroidRuntime(710): Caused by: java.lang.NullPointerException
11-27 13:21:57.917: E/AndroidRuntime(710): at timesheetdb.Displays.TimesheetEdit.onCreate(TimesheetEdit.java:73)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.Activity.performCreate(Activity.java:5008)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-27 13:21:57.917: E/AndroidRuntime(710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-27 13:21:57.917: E/AndroidRuntime(710): ... 11 more