addView android force close

时间:2013-03-05 12:38:08

标签: android dynamic views

以下是我的代码。我正在尝试添加一个视图到我已经创建的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>

一切正常,但是当我按下添加视图时,我会收到一个强制关闭

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

4 个答案:

答案 0 :(得分:1)

根据您的代码,出现了什么问题

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

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

来获取它

应该像

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

答案 1 :(得分:0)

 TextView t=(TextView) findViewById(R.id.a_text_view);

您无法添加文本视图,因为它已经拥有您需要的父级

  1. 从xml布局中删除textview并动态创建textview并将其添加到linearlayout。
  2. 喜欢这个

    TextView t=new TextView(this);
    layout.addView(t);
    

    你在switch case语句中添加了break吗

     case R.id.add:
    
               Log.v("test","as");
                TextView t=new TextView(this);
              layout.addView(t);
    
     break;
    

答案 2 :(得分:0)

TextView t=(TextView) findViewById(R.id.a_text_view);

您无法添加文本视图,因为它已有父文件,从xml布局中删除。 你还需要在uiThread (onCreate())中写下以下几行。

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

然后,

 public void onClick(View v) { 
switch(v.getId()) { 
   case R.id.add:

   Log.v("test","as");
   TextView t=new TextView(yourActivity.this); 
   layout.addView(t,1);          } }

编辑: 完整的代码将如下所示,

LinearLayout layout;
Button btnAdd;
TextView txtView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    layout = (LinearLayout) findViewById(R.id.activity_dynamic);
    btnAdd = (Button) findViewById(R.id.add);

    btnAdd.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()) { 
       case R.id.add:
       Log.v("test","as");
       TextView t=new TextView(this); 
       layout.addView(t,1);          
    }
}

希望得到这个帮助。

答案 3 :(得分:0)

在代码中添加this.addview(layout)