以下是我的代码。我正在尝试向我已经创建的 LinearLayout *activity_dynamic* 添加一个视图
public void onClick(View v)
{
switch(v.getId()) {
case R.id.add:
Log.v("test","as");
TextView t=(TextView) findViewById(R.id.a_text_view);
LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_dynamic);
layout .addView(t, 1);
}
}
activity_dynamic.xml 的代码是:
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="22dp"
android:text="Add View" />
</LinearLayout>
一切正常,但是当我按下 Add View 时,我强制关闭
LogCat 读数如下:
03-05 18:05:54.639: E/Trace(10003): error opening trace file: No such file or directory (2)
03-05 18:05:55.169: D/libEGL(10003): loaded /system/lib/egl/libEGL_adreno200.so
03-05 18:05:55.179: D/libEGL(10003): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-05 18:05:55.179: D/libEGL(10003): loaded /system/lib/egl/libGLESv2_adreno200.so
03-05 18:05:55.189: I/Adreno200-EGL(10003): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
03-05 18:05:55.189: I/Adreno200-EGL(10003): Build Date: 07/31/12 Tue
03-05 18:05:55.189: I/Adreno200-EGL(10003): Local Branch:
03-05 18:05:55.189: I/Adreno200-EGL(10003): Remote Branch: quic/master
03-05 18:05:55.189: I/Adreno200-EGL(10003): Local Patches: NONE
03-05 18:05:55.189: I/Adreno200-EGL(10003): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 + NOTHING
03-05 18:05:55.319: D/OpenGLRenderer(10003): Enabling debug mode 0
03-05 18:05:59.624: V/test(10003): as
03-05 18:05:59.624: D/AndroidRuntime(10003): Shutting down VM
03-05 18:05:59.624: W/dalvikvm(10003): threadid=1: thread exiting with uncaught exception (group=0x40d5e300)
03-05 18:05:59.874: E/AndroidRuntime(10003): FATAL EXCEPTION: main
03-05 18:05:59.874: E/AndroidRuntime(10003): java.lang.NullPointerException
03-05 18:05:59.874: E/AndroidRuntime(10003): at com.example.database.Dynamic.onClick(Dynamic.java:52)
03-05 18:05:59.874: E/AndroidRuntime(10003): at android.view.View.performClick(View.java:4084)
03-05 18:05:59.874: E/AndroidRuntime(10003): at android.view.View$PerformClick.run(View.java:16966)
03-05 18:05:59.874: E/AndroidRuntime(10003): at android.os.Handler.handleCallback(Handler.java:615)
03-05 18:05:59.874: E/AndroidRuntime(10003): at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 18:05:59.874: E/AndroidRuntime(10003): at android.os.Looper.loop(Looper.java:137)
03-05 18:05:59.874: E/AndroidRuntime(10003): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-05 18:05:59.874: E/AndroidRuntime(10003): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 18:05:59.874: E/AndroidRuntime(10003): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 18:05:59.874: E/AndroidRuntime(10003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-05 18:05:59.874: E/AndroidRuntime(10003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-05 18:05:59.874: E/AndroidRuntime(10003): at dalvik.system.NativeStart.main(Native Method)
为什么会这样?
感谢您的帮助,谢谢:D