我已经为我的活动添加了三个动态视图,现在我想为其中一个添加动态复选框,但它给我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>
这是动态视图的代码
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;
}
任何人都可以告诉我们java代码的错误,提前谢谢。 编辑: 这是logcat报告
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)
在CheckboxLayout.addView(cb)中发生了nullPointerException错误。