現在、各項目が現在の経過時間を表すリストビューがあります。私がやりたいのは、ユーザーがラップボタンをクリックするたびに、現在のストップウォッチ時間をリストに追加することです。それを実行してラップを押すと、何も起こりません。私はそれを次のように設定しました:
public class MainActivity extends Activity {
ArrayList<String> list =new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView myList = (ListView)findViewById(R.id.list);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
myList.setAdapter(adapter);
final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
final ImageButton buttonLap = (ImageButton)findViewById(R.id.lap);
buttonLap.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
String s = "" + (SystemClock.elapsedRealtime() - myChronometer.getBase());
list.add(s);
adapter.notifyDataSetChanged();
}});
}
}
これはlogcatです:
12-14 09:46:13.557: E/AndroidRuntime(954): FATAL EXCEPTION: main
12-14 09:46:13.557: E/AndroidRuntime(954): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.os.Looper.loop(Looper.java:137)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-14 09:46:13.557: E/AndroidRuntime(954): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 09:46:13.557: E/AndroidRuntime(954): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 09:46:13.557: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-14 09:46:13.557: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-14 09:46:13.557: E/AndroidRuntime(954): at dalvik.system.NativeStart.main(Native Method)
12-14 09:46:13.557: E/AndroidRuntime(954): Caused by: java.lang.NullPointerException
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.Activity.findViewById(Activity.java:1825)
12-14 09:46:13.557: E/AndroidRuntime(954): at com.example.myfirstapp.MainActivity.<init>(MainActivity.java:22)
12-14 09:46:13.557: E/AndroidRuntime(954): at java.lang.Class.newInstanceImpl(Native Method)
12-14 09:46:13.557: E/AndroidRuntime(954): at java.lang.Class.newInstance(Class.java:1319)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
12-14 09:46:13.557: E/AndroidRuntime(954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
12-14 09:46:13.557: E/AndroidRuntime(954): ... 11 more