2015-06-26 73 views
1

我是Android新手,无法知道如何动态地将按钮添加到预先存在的布局。我将来自SO的另一个问题的一些示例代码修补到了hello world默认项目中。在Android Studio中动态创建按钮时遇到问题

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的“调试”过滤器非常冗长,即使程序没有运行也会不断更新。当我以调试模式运行它时,手机在消息“应用程序停止工作”出现之前显示了一小段时间的错误。

+1

http://stackoverflow.com/questions/10418929/add-view-programmatically-to-relativelayout的可能的复制。 –

回答

4

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

在设置内容视图之前,您正在执行findViewById。这意味着没有观点可以找到。所以当你尝试使用那个视图时,你会得到一个空指针例外

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