Android アプリ開発は初めてで、小さな問題があります。
例に従って、To Do リスト アプリを作成しようとしました。ただし、正確にコピーしても機能しません。
実行するとエラーは表示されませんが、空白の画面が表示され、数秒後に閉じて、「残念ながら、ToDoList が停止しました」と表示されます。
この問題が何であるか知っている人はいますか?
コード
package com.paad.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = (ListView)findViewById(R.id.ListView);
final EditText myEditText = (EditText) findViewById(R.id.EditText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,todoItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
@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;
}
}
レイアウト XML ファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
android:contentDescription="@string/addItemContentDescription"
/>
<ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
ログ:
02-10 20:03:53.850: D/AndroidRuntime(24220): Shutting down VM
02-10 20:03:53.850: W/dalvikvm(24220): threadid=1: thread exiting with uncaught exception (group=0x40f0a2a0)
02-10 20:03:53.850: E/AndroidRuntime(24220): FATAL EXCEPTION: main
02-10 20:03:53.850: E/AndroidRuntime(24220): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.todolist/com.paad.todolist.MainActivity}: java.lang.NullPointerException
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.os.Looper.loop(Looper.java:137)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-10 20:03:53.850: E/AndroidRuntime(24220): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 20:03:53.850: E/AndroidRuntime(24220): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 20:03:53.850: E/AndroidRuntime(24220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-10 20:03:53.850: E/AndroidRuntime(24220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-10 20:03:53.850: E/AndroidRuntime(24220): at dalvik.system.NativeStart.main(Native Method)
02-10 20:03:53.850: E/AndroidRuntime(24220): Caused by: java.lang.NullPointerException
02-10 20:03:53.850: E/AndroidRuntime(24220): at com.paad.todolist.MainActivity.onCreate(MainActivity.java:30)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.Activity.performCreate(Activity.java:5206)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
02-10 20:03:53.850: E/AndroidRuntime(24220): ... 11 more