プログラムで 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