在 Android Studio 中动态创建按钮时遇到问题

Having trouble dynamically creating buttons in Android Studio

我是 Android 的新手,我不知道如何将按钮动态添加到预先存在的布局中。我将从 SO 上的另一个问题中找到的一些示例代码拼凑到一个 hello world 默认项目中。

onCreate 方法:

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

        Button myButton = new Button(this);
        myButton.setText("Add Me");

        RelativeLayout ll = (RelativeLayout)findViewById(R.id.main_layout);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);

        setContentView(R.layout.activity_main);
    }

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/main_layout"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/cat_1"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="26dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/cat_2"
        android:layout_alignBottom="@+id/cat_1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="65dp"
        android:layout_marginEnd="65dp" />

</RelativeLayout>

当我注释掉添加按钮部分时该项目有效,所以我知道这是问题所在。我会粘贴一些调试信息,但是 logcat 的 'debug' 过滤器非常冗长并且不断更新,即使程序不是 运行。当我 运行 它处于调试模式时, phone 在消息 "The app has stopped working" 出现之前的几分之一秒内显示错误。

您在设置内容视图之前执行 findViewById。这意味着找不到任何视图。所以当你尝试使用那个视图时,你会得到一个空指针异常

setContentView 应该在 super 之后立即完成。否则任何时候你调用 findViewById 都会 return 一个空指针。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button myButton = new Button(this);
    myButton.setText("Add Me");

    RelativeLayout ll = (RelativeLayout)findViewById(R.id.main_layout);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
}
LinearLayout ll = (LinearLayout) findViewById(R.id.main_layout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

for(int i=0;i<30;i++){
    Button myButton = new Button(this);
    myButton.setText("Add Me");
    myButton.setId(i);
    ll.addView(myButton, lp);
}