0

因此,每次单击按钮时,我仍在尝试添加 LinearLayout。

现在我发现的方式只显示我添加的第一个,所有其他的似乎都没有添加或不在活动的可见表面上。

首先,我的类CustomLinearLayout

public class CustomLinearLayout extends LinearLayout{

    public View v1, v2, v3, v4;
    public CheckBox cb;
    public TextView tv1, tv2;
    public EditText et1, et2;

    public CustomLinearLayout(int set, Context context){
        super(context);
        setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        cb = new CheckBox(context);
        cb.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));

        tv1 = new TextView(context);
        tv1.setText(" Set " + set);
        tv1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));

        v1 = new View(context);
        v1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));

        et1 = new EditText(context);
        et1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));

        v2 = new View(context);
        v2.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));

        tv2 = new TextView(context);
        tv2.setText(":");

        v3 = new View(context);
        v3.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));

        et2 = new EditText(context);
        et1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));

        v4 = new View(context);
        v4.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));

        addView(cb);
        addView(tv1);
        addView(v2);
        addView(et1);
        addView(v3);
        addView(tv2);
        addView(v1);
        addView(et2);
        addView(v4);
    }
}

我的布局 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lin_ppe2_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp" 
    android:orientation="vertical">
    <TextView style="@style/TextHeader"
        android:id="@+id/tv_ppe2_header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_pingpong_entry"
        />

      <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="+"
        android:onClick="onClickAddNewSet"
        />
</LinearLayout>

和活动本身

public class ChallengeActivity extends Activity {

    Vector<CustomLinearLayout> layouts = new Vector<CustomLinearLayout>();
    int counter = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_pingpong_entry2);

        for(int i=1; i<8; i++){
            layouts.add(new CustomLinearLayout(i, this));
        }
    }

    public void onClickAddNewSet(View v){
        LinearLayout lin = (LinearLayout)findViewById(R.id.lin_ppe2_main);
        lin.addView(layouts.elementAt(counter));
        counter++;
    }
}

以下是我多次单击后得到的结果,无论多久(布局本身还没有设计得很好,但这是次要的):

在此处输入图像描述

我认为由于它是一个方向=垂直的线性布局,它应该自动将每个新视图添加到最后一个视图下方。为什么不这样做?为什么第一个布局工作正常,但在第二个之后什么都没有显示?我还尝试将方向设置:垂直于 CustomLinearLayout,但随后什么也没有出现。

更新: 按照建议,我尝试使用 hierarchyviewer。当我尝试添加第二个视图时,我收到以下错误消息:

09-26 18:40:54.489: E/AndroidRuntime(7341): FATAL EXCEPTION: main
09-26 18:40:54.489: E/AndroidRuntime(7341): java.lang.IllegalStateException: Could not execute method of the activity
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.View$1.onClick(View.java:2144)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.View.performClick(View.java:2485)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.View$PerformClick.run(View.java:9080)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.os.Handler.handleCallback(Handler.java:587)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.os.Looper.loop(Looper.java:123)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at java.lang.reflect.Method.invoke(Method.java:507)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at dalvik.system.NativeStart.main(Native Method)
09-26 18:40:54.489: E/AndroidRuntime(7341): Caused by: java.lang.reflect.InvocationTargetException
09-26 18:40:54.489: E/AndroidRuntime(7341):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at java.lang.reflect.Method.invoke(Method.java:507)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.View$1.onClick(View.java:2139)
09-26 18:40:54.489: E/AndroidRuntime(7341):     ... 11 more
09-26 18:40:54.489: E/AndroidRuntime(7341): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
09-26 18:40:54.489: E/AndroidRuntime(7341):     at challenge.main.ChallengeActivity.onClickAddNewSet(ChallengeActivity.java:49)
09-26 18:40:54.489: E/AndroidRuntime(7341):     ... 14 more

添加一个自定义布局后,hierarchyviewer 说如下(红色标记的是自定义视图),在应用程序第二次崩溃之后。

在此处输入图像描述

4

0 回答 0