Android 向动态添加的视图添加动态复选框时遇到困难

Android 向动态添加的视图添加动态复选框时遇到困难,android,nullpointerexception,Android,Nullpointerexception,我已经在我的活动中添加了三个动态视图,现在我想在其中一个视图中添加动态复选框,但这会给我带来nullPointerException错误。这是我的视图的xml布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout3" android:l

我已经在我的活动中添加了三个动态视图,现在我想在其中一个视图中添加动态复选框,但这会给我带来nullPointerException错误。这是我的视图的xml布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <TextView
            android:id="@+id/tvMod"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="What does this symptom occur or when is it worse?"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:id="@+id/Checkbox_Layout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >"

        </LinearLayout>



</LinearLayout>

nullPointerException错误发生在CheckboxLayout.addViewcb。

如果您不发布带有nullPointerException的logcat输出,以及它发生在哪里,人们不会浪费几个小时来尝试解决它:添加了logcat:@rupps现在请您浪费几分钟来帮助我好吗当然但很难说清楚,因为您引用了我不知道的布局,比如R.id.linearlayout2。名字有点乱。问题很明显是checkboxLayout3是空的,正如logcat明显告诉您的那样。尝试更改LinearLayout checkboxLayout3=LinearLayout findViewByIdR.id.Checkbox\u Layout3;对于LinearLayout checkboxLayout3=LinearLayout modLayout.findViewByIdR.id.Checkbox\u Layout3,也就是说,从膨胀视图而不是从根视图中查找视图。当modlayout==null时,也可以从代码中正确分配它,以便modlayout.findviewbyd不会crash@rupp,我找到了解决方案,我得到了nullPointerException,因为data_mod没有正确初始化,所以checkboxLayout3为空:
public void onClick(View v) {
    switch(v.getId()) {
case R.id.mod:
        LinearLayout modLayout = (LinearLayout)findViewById(R.id.layout3);
        if(modLayout== null) {
             LinearLayout myLayout3 = (LinearLayout)findViewById(R.id.linearLayout2);
             View hiddenInfo3 = getLayoutInflater().inflate(R.layout.taking_modalation, myLayout3, false);
             myLayout3.addView(hiddenInfo3);
        }

        LinearLayout checkboxLayout3 = (LinearLayout) findViewById(R.id.Checkbox_Layout3);
         for (int i = 0; i < data_mod.length; i++) {
                CheckBox cbmod = new CheckBox(getApplicationContext());
                cbmod.setTextColor(Color.BLACK);
                cbmod.setButtonDrawable(id);
                cbmod.setText(data_mod[i]);
                checkboxLayout3.addView(cbmod);
            }
        break;
    }
09-14 02:06:14.380: D/AndroidRuntime(1019): Shutting down VM
09-14 02:06:14.380: W/dalvikvm(1019): threadid=1: thread exiting with uncaught exception (group=0xb4a4eba8)
09-14 02:06:14.420: E/AndroidRuntime(1019): FATAL EXCEPTION: main
09-14 02:06:14.420: E/AndroidRuntime(1019): Process: com.example.mydb, PID: 1019
09-14 02:06:14.420: E/AndroidRuntime(1019): java.lang.NullPointerException
09-14 02:06:14.420: E/AndroidRuntime(1019):     at com.example.mydb.DetailActivity.onClick(DetailActivity.java:198)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at android.view.View.performClick(View.java:4438)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at android.view.View$PerformClick.run(View.java:18422)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at android.os.Handler.handleCallback(Handler.java:733)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at android.os.Looper.loop(Looper.java:136)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at java.lang.reflect.Method.invoke(Method.java:515)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-14 02:06:14.420: E/AndroidRuntime(1019):     at dalvik.system.NativeStart.main(Native Method)