1

以下是我的代码。我正在尝试向我已经创建的 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

4

4 回答 4

1

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

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

你得到带有布局的LinearLayout,你应该使用它R.id.yourlayoutid

应该像

 layout = (LinearLayout) findViewById(R.id.yourlayoutid);
于 2013-03-05T17:36:41.577 回答
0
 TextView t=(TextView) findViewById(R.id.a_text_view);

您不能添加文本视图,因为它已经有一个您需要的父视图

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

这样做

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

你有没有在你的 switch case 语句中添加中断

 case R.id.add:

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

 break;
于 2013-03-05T12:46:01.693 回答
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);          
    }
}

希望这有帮助。

于 2013-03-05T12:51:05.557 回答
0

添加this.addview(layout)代码

于 2013-03-05T13:02:25.830 回答