gpt4 book ai didi

android - addView android强制关闭

转载 作者:行者123 更新时间:2023-11-30 03:44:26 25 4
gpt4 key购买 nike

以下是我的代码。我正在尝试向我已经创建的 LinearLayout *activity_dynamic* 添加一个 View

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>

一切正常,但是当我按下添加 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

最佳答案

根据你的代码,错误是

  layout = (LinearLayout) findViewById(R.layout.activity_dynamic);

您正在获取带布局的 LinearLayout,您应该使用 R.id.yourlayoutid

应该是这样的

 layout = (LinearLayout) findViewById(R.id.yourlayoutid);

关于android - addView android强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com