0

我有一个带有 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 正确获取。

@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
4

0 回答 0