简体   繁体   English

在Android Studio中动态创建按钮时遇到麻烦

[英]Having trouble dynamically creating buttons in Android Studio

I'm new to Android and I can't figure out how to dynamically add a button to a pre-existing layout. 我是Android的新手,我不知道如何动态地向现有布局添加按钮。 I patched together some example code I found from another question on SO into a hello world default project. 我将在SO上的另一个问题中找到的一些示例代码修补到一个hello world默认项目中。

onCreate method: 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);
    }

Layout: 布局:

<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>

The project works when I comment out the adding a button part so I know that's the problem. 当我注释掉添加按钮部分时,该项目将正常工作,所以我知道这就是问题所在。 And I would have pasted some debug information but the 'debug' filter for the logcat was extremely verbose and constantly updating even if the program wasn't running. 而且我会粘贴一些调试信息,但是logcat的“ debug”过滤器非常冗长,即使程序没有运行也要不断更新。 When I ran it in debug mode the phone displayed an error for a fraction of a second before the message "The app has stopped working" came up. 当我在调试模式下运行手机时,手机显示错误消息几分之一秒,然后出现消息“应用程序已停止工作”。

setContentView should be done right after super. setContentView应该在super之后立即完成。 Otherwise anytime you call findViewById will return a null pointer. 否则,任何时候调用findViewById都将返回空指针。

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);
}

You're doing findViewById before setting the content view. 在设置内容视图之前,您正在执行findViewById。 That means there's no views to find. 这意味着找不到任何视图。 So when you try to use that view, you get a null pointer exception 因此,当您尝试使用该视图时,会出现空指针异常

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM