我目前有一个列表视图,其中每个项目代表当前经过的时间。我想做的是每次用户单击 Lap 按钮时,都会将当前秒表时间添加到列表中。当我运行它并按下 lap 时没有任何反应。我是这样设置的:
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();
}});
}
}
这是日志:
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