我正在尝试以编程方式向LinearLayout添加按钮,但应用程序崩溃:
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/main_view" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Button"
android:onClick="onClickBtn" />
</LinearLayout>
这是代码:
package com.chovanec.runo;
import android.os.Bundle; // and more
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClickBtn(View v)
{
Button btn = new Button(getBaseContext());
btn.setText("New Button");
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
l.addView(btn);
}
}
logcat的:
04-14 03:09:00.150: D/AndroidRuntime(7375): Shutting down VM
04-14 03:09:00.150: W/dalvikvm(7375): threadid=1: thread exiting with uncaught exception (group=0x415102a0)
04-14 03:09:00.155: E/AndroidRuntime(7375): FATAL EXCEPTION: main
04-14 03:09:00.155: E/AndroidRuntime(7375): java.lang.IllegalStateException: Could not execute method of the activity
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.view.View$1.onClick(View.java:3691)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.view.View.performClick(View.java:4211)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.view.View$PerformClick.run(View.java:17267)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.os.Handler.handleCallback(Handler.java:615)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.os.Looper.loop(Looper.java:137)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-14 03:09:00.155: E/AndroidRuntime(7375): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 03:09:00.155: E/AndroidRuntime(7375): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 03:09:00.155: E/AndroidRuntime(7375): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-14 03:09:00.155: E/AndroidRuntime(7375): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-14 03:09:00.155: E/AndroidRuntime(7375): at dalvik.system.NativeStart.main(Native Method)
04-14 03:09:00.155: E/AndroidRuntime(7375): Caused by: java.lang.reflect.InvocationTargetException
04-14 03:09:00.155: E/AndroidRuntime(7375): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 03:09:00.155: E/AndroidRuntime(7375): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 03:09:00.155: E/AndroidRuntime(7375): at android.view.View$1.onClick(View.java:3686)
04-14 03:09:00.155: E/AndroidRuntime(7375): ... 11 more
04-14 03:09:00.155: E/AndroidRuntime(7375): Caused by: java.lang.NullPointerException
04-14 03:09:00.155: E/AndroidRuntime(7375): at com.chovanec.runo.MainActivity.onClickBtn(MainActivity.java:37)
04-14 03:09:00.155: E/AndroidRuntime(7375): ... 14 more
答案 0 :(得分:2)
应为LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
您的线性布局中也会错过android:orientation="vertical"
,这就是新按钮未显示的原因。除非您希望按钮并排显示,否则您必须在按钮xml中将android:layout_width="fill_parent"
更改为android:layout_width="wrap_content"
你不能使用
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
除非是相对布局。
答案 1 :(得分:1)
尝试使用:
Button btn = new Button(this);
而不是:
Button btn = new Button(getBaseContext());
<强>更新强>
并尝试将按钮添加到内部LinearLayout
:
LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
而不是:
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
我认为您不能直接在活动布局中添加按钮。
答案 2 :(得分:1)
您的LinearLayout l
实际上是 null 。
改变这个:
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
对此:
LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
答案 3 :(得分:0)
问题出在这里
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
l.addView(btn);
findViewById需要一个view元素的id你给它布局id。
试试这个
LinearLayout l = (LinearLayout) findViewById(android.R.id.content);
或
LinearLayout l = (LinearLayout) findViewById(android.R.id.content).getRootView()