在这里练习基本的 android 示例我的代码中出现空指针异常,如何解决这个问题。在此处附加代码。
public void onClick(View v) {
switch (v.getId()) {
case R.id.toggleButton1:
// TODO Auto-generated method stub
try {
if (t1.isChecked()) {
StringBuilder s1 = new StringBuilder();
s1.append(b1.getText());
Toast.makeText(getBaseContext(), s1.toString(),
Toast.LENGTH_SHORT);
} else {
TextView t1 = (TextView) findViewById(R.id.t1);
StringBuilder s2 = new StringBuilder();
s2.append(t1.getText());
Toast.makeText(getBaseContext(), t1.toString(),
Toast.LENGTH_SHORT);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case R.id.button1:
try {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.mkyong.com"));
startActivity(browserIntent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
不明白问题出现在哪一行,这里也附上堆栈跟踪
07-06 05:56:54.373: D/AndroidRuntime(940): Shutting down VM
07-06 05:56:54.373: W/dalvikvm(940): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
07-06 05:56:54.393: E/AndroidRuntime(940): FATAL EXCEPTION: main
07-06 05:56:54.393: E/AndroidRuntime(940): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xxx/com.example.xxx.MainActivity}: java.lang.NullPointerException
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.os.Looper.loop(Looper.java:137)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-06 05:56:54.393: E/AndroidRuntime(940): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 05:56:54.393: E/AndroidRuntime(940): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 05:56:54.393: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-06 05:56:54.393: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-06 05:56:54.393: E/AndroidRuntime(940): at dalvik.system.NativeStart.main(Native Method)
07-06 05:56:54.393: E/AndroidRuntime(940): Caused by: java.lang.NullPointerException
07-06 05:56:54.393: E/AndroidRuntime(940): at com.example.xxx.MainActivity.onCreate(MainActivity.java:26)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.Activity.performCreate(Activity.java:4465)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-06 05:56:54.393: E/AndroidRuntime(940): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-06 05:56:54.393: E/AndroidRuntime(940): ... 11 more
07-06 06:01:54.493: I/Process(940): Sending signal. PID: 940 SIG: 9