为什么我的程序会停止运行
照着老师的代码敲得,但是程序会奔溃掉。删掉这节课的代码,程序就可以正常运行。
代码贴在下边
package com.example.secondapp; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.os.Build; public class MainActivity extends ActionBarActivity { private Button loginbutton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginbutton = (Button) findViewById(R.id.button1); loginbutton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { System.out.println("Buttoned"); } }); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } }
运行日志
02-13 22:59:01.449: D/gralloc_goldfish(1159): Emulator without GPU emulation detected. 02-13 23:03:35.629: D/AndroidRuntime(1205): Shutting down VM 02-13 23:03:35.639: W/dalvikvm(1205): threadid=1: thread exiting with uncaught exception (group=0xb3b0bba8) 02-13 23:03:35.639: E/AndroidRuntime(1205): FATAL EXCEPTION: main 02-13 23:03:35.639: E/AndroidRuntime(1205): Process: com.example.secondapp, PID: 1205 02-13 23:03:35.639: E/AndroidRuntime(1205): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapp/com.example.secondapp.MainActivity}: java.lang.NullPointerException 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.ActivityThread.access$800(ActivityThread.java:135) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.os.Handler.dispatchMessage(Handler.java:102) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.os.Looper.loop(Looper.java:136) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-13 23:03:35.639: E/AndroidRuntime(1205): at java.lang.reflect.Method.invokeNative(Native Method) 02-13 23:03:35.639: E/AndroidRuntime(1205): at java.lang.reflect.Method.invoke(Method.java:515) 02-13 23:03:35.639: E/AndroidRuntime(1205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-13 23:03:35.639: E/AndroidRuntime(1205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-13 23:03:35.639: E/AndroidRuntime(1205): at dalvik.system.NativeStart.main(Native Method) 02-13 23:03:35.639: E/AndroidRuntime(1205): Caused by: java.lang.NullPointerException 02-13 23:03:35.639: E/AndroidRuntime(1205): at com.example.secondapp.MainActivity.onCreate(MainActivity.java:29) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.Activity.performCreate(Activity.java:5231) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 02-13 23:03:35.639: E/AndroidRuntime(1205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 02-13 23:03:35.639: E/AndroidRuntime(1205): ... 11 more 02-13 23:03:39.409: I/Process(1205): Sending signal. PID: 1205 SIG: 9 02-13 23:04:20.589: D/gralloc_goldfish(1253): Emulator without GPU emulation detected. 02-13 23:16:17.019: D/AndroidRuntime(1298): Shutting down VM 02-13 23:16:17.019: W/dalvikvm(1298): threadid=1: thread exiting with uncaught exception (group=0xb3b0bba8) 02-13 23:16:17.029: E/AndroidRuntime(1298): FATAL EXCEPTION: main 02-13 23:16:17.029: E/AndroidRuntime(1298): Process: com.example.secondapp, PID: 1298 02-13 23:16:17.029: E/AndroidRuntime(1298): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapp/com.example.secondapp.MainActivity}: java.lang.NullPointerException 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.ActivityThread.access$800(ActivityThread.java:135) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.os.Handler.dispatchMessage(Handler.java:102) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.os.Looper.loop(Looper.java:136) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-13 23:16:17.029: E/AndroidRuntime(1298): at java.lang.reflect.Method.invokeNative(Native Method) 02-13 23:16:17.029: E/AndroidRuntime(1298): at java.lang.reflect.Method.invoke(Method.java:515) 02-13 23:16:17.029: E/AndroidRuntime(1298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-13 23:16:17.029: E/AndroidRuntime(1298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-13 23:16:17.029: E/AndroidRuntime(1298): at dalvik.system.NativeStart.main(Native Method) 02-13 23:16:17.029: E/AndroidRuntime(1298): Caused by: java.lang.NullPointerException 02-13 23:16:17.029: E/AndroidRuntime(1298): at com.example.secondapp.MainActivity.onCreate(MainActivity.java:30) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.Activity.performCreate(Activity.java:5231) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 02-13 23:16:17.029: E/AndroidRuntime(1298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 02-13 23:16:17.029: E/AndroidRuntime(1298): ... 11 more 02-13 23:17:23.039: I/Process(1298): Sending signal. PID: 1298 SIG: 9 02-13 23:17:33.299: D/AndroidRuntime(1345): Shutting down VM 02-13 23:17:33.299: W/dalvikvm(1345): threadid=1: thread exiting with uncaught exception (group=0xb3b0bba8) 02-13 23:17:33.309: E/AndroidRuntime(1345): FATAL EXCEPTION: main 02-13 23:17:33.309: E/AndroidRuntime(1345): Process: com.example.secondapp, PID: 1345 02-13 23:17:33.309: E/AndroidRuntime(1345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapp/com.example.secondapp.MainActivity}: java.lang.NullPointerException 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.ActivityThread.access$800(ActivityThread.java:135) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.os.Handler.dispatchMessage(Handler.java:102) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.os.Looper.loop(Looper.java:136) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-13 23:17:33.309: E/AndroidRuntime(1345): at java.lang.reflect.Method.invokeNative(Native Method) 02-13 23:17:33.309: E/AndroidRuntime(1345): at java.lang.reflect.Method.invoke(Method.java:515) 02-13 23:17:33.309: E/AndroidRuntime(1345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-13 23:17:33.309: E/AndroidRuntime(1345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-13 23:17:33.309: E/AndroidRuntime(1345): at dalvik.system.NativeStart.main(Native Method) 02-13 23:17:33.309: E/AndroidRuntime(1345): Caused by: java.lang.NullPointerException 02-13 23:17:33.309: E/AndroidRuntime(1345): at com.example.secondapp.MainActivity.onCreate(MainActivity.java:25) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.Activity.performCreate(Activity.java:5231) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 02-13 23:17:33.309: E/AndroidRuntime(1345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 02-13 23:17:33.309: E/AndroidRuntime(1345): ... 11 more