尝试将TextView添加到RelativeLayout时出现NullPointerException

时间:2014-04-09 00:34:06

标签: android android-layout android-view

我尝试使用for loop填充具有多个元素的RelativeLayout,其中字符串数组是更改因子。错误使我将其修改为更简单的代码,这仍然给我带来麻烦。当我尝试添加TextView时,我收到了NullPointerException。

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

此代码在活动的片段类中运行。

根据请求进行Stacktrace:

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

答案:我的片段类(此代码运行的地方)有一个由其布局定义的rootView。该修复程序已更改为rootView.findViewById(addGradeTypesLayoutId)

2 个答案:

答案 0 :(得分:1)

  1. 确保您的代码在上面提供的代码之前调用了Activity.setContentView()。
  2. 如果是这样,请确保传递给setContentView()的视图或布局实际上包含RelativeLayout ID AddGradeTypesLayout
  3. 如果代码片段位于onCreateView()中,则不应假定Activity布局已准备好直接添加,因为onCreateView()在完全创建活动之前被称为。相反,你应该:(a)更新你的布局XML以指定片段在AddGradeTypesLayout RelativeLayout中,并且(b)让你的onCreateView()返回新的TextView [而不是在活动上调用addView()布局对象,可以为null]。这种方法的Here is an example(寻找 TextFragment )。

答案 1 :(得分:-2)

您应该使用findViewById()来查找textView的id。