Note that there are some explanatory texts on larger screens.

plurals
  1. POPrepoulating Components With Data In Android Activity onCreate without Getting null pointer exception
    text
    copied!<p>I have a FragmentActivity with a ViewPager. Inside that ViewPager are a total of 3 different views.</p> <p>The user selects a row containing information. Once selected, the FragmentActivity shall load and present the data in the views within the ViewPager.</p> <p>I want the data to be preloaded, so when the activity finally appears, everything is in the correct widget. I am choosing to do this in the onCreate method of the FragmentActivity. However, when I try to access any of the widgets on the views in the ViewPager, I get a NullPointerException.</p> <p>I do not know how to get around this.</p> <p>As Per Request:</p> <p>This block of code, handles calls the FragmentActivity - the "co" is a serialized object containing the information that will be displayed.</p> <pre><code> 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)); </code></pre> <p>The next set of code is the onCreate method of the Fragmentactivity. The Log.i at the end is just for testing purposes, but that is also where my NullPointerException occurs. I assume because for some reason, the View has not been obtained properly from the ViewPager.</p> <pre><code>@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(); } </code></pre> <p>And finally, the LogCat corresponding to this error is:</p> <pre><code> 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload