Java 将按钮添加到LinearLayout don';行不通
我试图通过编程方式将按钮添加到LinearLayout,但应用程序崩溃: activity_main.xml:Java 将按钮添加到LinearLayout don';行不通,java,android,Java,Android,我试图通过编程方式将按钮添加到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="
<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>
日志:
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
尝试使用:
Button btn = new Button(this);
而不是:
Button btn = new Button(getBaseContext());
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
更新:
并尝试将按钮添加到内部线性布局中
:
LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
而不是:
Button btn = new Button(getBaseContext());
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
我认为您不能直接在活动布局中添加按钮。尝试使用:
Button btn = new Button(this);
而不是:
Button btn = new Button(getBaseContext());
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
更新:
并尝试将按钮添加到内部线性布局中
:
LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
而不是:
Button btn = new Button(getBaseContext());
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
我认为您不能直接在活动布局中添加按钮。应该是
LinearLayout l=(LinearLayout)findViewById(R.id.main\u视图)代码>
在线性布局中,您也会错过android:orientation=“vertical”
,这就是为什么新按钮没有出现的原因。除非您希望按钮并排显示,否则必须将按钮xml中的android:layout\u width=“fill\u parent”更改为android:layout\u width=“wrap\u content”
你不能使用
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
除非是相对布局。应该是
LinearLayout l=(LinearLayout)findViewById(R.id.main\u视图)代码>
在线性布局中,您也会错过android:orientation=“vertical”
,这就是为什么新按钮没有出现的原因。除非您希望按钮并排显示,否则必须将按钮xml中的android:layout\u width=“fill\u parent”更改为android:layout\u width=“wrap\u content”
你不能使用
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
除非是相对布局。您的
线性布局l
实际上是空的
更改此项:
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
为此:
LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
您的
线性布局l
实际上是null
更改此项:
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
为此:
LinearLayout l = (LinearLayout) findViewById(R.id.main_view);
问题就在这里
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
l.addView(btn);
findViewById需要一个视图元素的id,您给它一个布局id
试试这个
LinearLayout l = (LinearLayout) findViewById(android.R.id.content);
或
问题就在这里
LinearLayout l = (LinearLayout) findViewById(R.layout.activity_main);
l.addView(btn);
findViewById需要一个视图元素的id,您给它一个布局id
试试这个
LinearLayout l = (LinearLayout) findViewById(android.R.id.content);
或
哪一行是你的第37行哪一行是你的第37行这没有帮助,仍然崩溃这没有帮助,仍然崩溃给出一个已经给出了两次的答案有什么意义?这个更改很好(防止崩溃),但新按钮没有出现,另一个呢?隐形?@EmilAdz通过观察别人的答案来编辑自己的答案有什么意义?我不是在说我的答案@Hoan Nguyen也给出了同样的答案。给出一个已经给出了两次的答案有什么意义?这个改变很好(防止崩溃),但新的按钮没有出现,而另一个呢?隐形?@EmilAdz通过观察别人的答案来编辑自己的答案有什么意义?我不是在说我的答案@Hoan Nguyen也给出了相同的答案。我编辑了我的答案以显示新按钮没有出现的原因。我编辑了我的答案以显示新按钮没有出现的原因。