Question

I'm trying to populate a RelativeLayout with multiple elements by using a for loop in which a string array is the changing factor. Errors made me trim this down to a simpler code, which is still giving me trouble. I'm getting a NullPointerException when I try to add the TextView, t.

int addGradeTypesLayoutId = R.id.AddGradeTypesLayout;

         TextView t = new TextView(this.getActivity());
         RelativeLayout addGradeTypesLayout = (RelativeLayout) this.getActivity().findViewById(addGradeTypesLayoutId);
         RelativeLayout.LayoutParams tLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
         tLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);;
         t.setText("helloooo");
         t.setLayoutParams(tLayoutParams);
         t.setId(1);
         addGradeTypesLayout.addView(t);  //Exception here

This code runs within a fragment class in an activity.

Stacktrace as per request:

04-08 20:37:59.235: D/(2413): HostConnection::get() New Host Connection established 0xb84b56b0, tid 2413
04-08 20:37:59.415: W/EGL_emulation(2413): eglSurfaceAttrib not implemented
04-08 20:37:59.425: D/OpenGLRenderer(2413): Enabling debug mode 0
04-08 20:38:16.135: D/dalvikvm(2413): GC_FOR_ALLOC freed 121K, 7% free 3018K/3236K, paused 68ms, total 72ms
04-08 20:38:16.285: D/AndroidRuntime(2413): Shutting down VM
04-08 20:38:16.285: W/dalvikvm(2413): threadid=1: thread exiting with uncaught exception (group=0xb2a9bba8)
04-08 20:38:16.335: E/AndroidRuntime(2413): FATAL EXCEPTION: main
04-08 20:38:16.335: E/AndroidRuntime(2413): Process: com.NsouthProductions.gradetrackerpro, PID: 2413
04-08 20:38:16.335: E/AndroidRuntime(2413): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NsouthProductions.gradetrackerpro/com.NsouthProductions.gradetrackerpro.AddCourse}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.os.Looper.loop(Looper.java:136)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at java.lang.reflect.Method.invoke(Method.java:515)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at dalvik.system.NativeStart.main(Native Method)
04-08 20:38:16.335: E/AndroidRuntime(2413): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.Activity.setContentView(Activity.java:1929)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at com.NsouthProductions.gradetrackerpro.AddCourse.onCreate(AddCourse.java:43)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.Activity.performCreate(Activity.java:5231)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-08 20:38:16.335: E/AndroidRuntime(2413):     ... 11 more
04-08 20:38:16.335: E/AndroidRuntime(2413): Caused by: java.lang.NullPointerException
04-08 20:38:16.335: E/AndroidRuntime(2413):     at com.NsouthProductions.gradetrackerpro.AddCourse$AddCourseFragment.onCreateView(AddCourse.java:184)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1093)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1195)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
04-08 20:38:16.335: E/AndroidRuntime(2413):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
04-08 20:38:16.335: E/AndroidRuntime(2413):     ... 24 more
04-08 20:38:19.565: I/Process(2413): Sending signal. PID: 2413 SIG: 9

ANSWER: My fragment class (where this code runs) has a rootView defined by its layout. The fix was changing to rootView.findViewById(addGradeTypesLayoutId).

Was it helpful?

Solution

  1. Make sure your code called Activity.setContentView() before the code you provided above.
  2. If so, make sure the view or the layout that you passed to setContentView() actually contains a RelativeLayout with a AddGradeTypesLayout ID.
  3. If the snippet is in onCreateView(), you should not assume that the Activity layout is ready for you to add to directly, since onCreateView() is called before the Activity is completely created. Instead, you should: (a) update your layout XML to specify that the fragment is inside the AddGradeTypesLayout RelativeLayout, and (b) have your onCreateView() return the new TextView [instead of calling addView() on the activity's layout object, which could be null]. Here is an example of this approach (look for the TextFragment).

OTHER TIPS

You should use findViewById() to find id of the textView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top