Java 在Android Studio中动态创建按钮时遇到问题
我是Android新手,我不知道如何在预先存在的布局中动态添加按钮。我将从另一个问题中找到的一些示例代码拼凑到一个hello world默认项目中 onCreate方法:Java 在Android Studio中动态创建按钮时遇到问题,java,android,button,dynamic,android-relativelayout,Java,Android,Button,Dynamic,Android Relativelayout,我是Android新手,我不知道如何在预先存在的布局中动态添加按钮。我将从另一个问题中找到的一些示例代码拼凑到一个hello world默认项目中 onCreate方法: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button myButton = new Button(this); myButton.setTe
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>
布局:
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的“调试”过滤器非常冗长,即使程序没有运行,也会不断更新。当我在调试模式下运行时,手机显示了一个错误,持续了几分之一秒,然后出现了“应用程序已停止工作”的消息。在设置内容视图之前,您正在执行findViewById。这意味着找不到任何视图。因此,当您尝试使用该视图时,在设置内容视图之前,会出现空指针异常。这意味着找不到任何视图。因此,当您尝试使用该视图时,您会得到一个空指针异常
setContentView
应该在super之后立即执行。否则,无论何时调用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);
}
setContentView
应该在超级链接之后立即执行。否则,无论何时调用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);
}
LinearLayout ll=(LinearLayout)findViewById(R.id.main\u布局);
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.WRAP_内容);
对于(int i=0;iLinearLayout ll=(LinearLayout)findViewById(R.id.main\u布局);
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.WRAP_内容);
对于(int i=0;i的可能副本。的可能副本)。