每当我运行我的应用程序时,我第一次从 Eclipse 运行它时运行良好。但是,如果我关闭应用程序并重新打开它,它将在 onCreate 上崩溃。这是LogCat:
10-10 02:50:00.665: E/AndroidRuntime(29564): FATAL EXCEPTION: main
10-10 02:50:00.665: E/AndroidRuntime(29564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appuccino.unitconverter/com.appuccino.unitconverter.MainActivity}: java.lang.NullPointerException
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.os.Looper.loop(Looper.java:137)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-10 02:50:00.665: E/AndroidRuntime(29564): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 02:50:00.665: E/AndroidRuntime(29564): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 02:50:00.665: E/AndroidRuntime(29564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
10-10 02:50:00.665: E/AndroidRuntime(29564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
10-10 02:50:00.665: E/AndroidRuntime(29564): at dalvik.system.NativeStart.main(Native Method)
10-10 02:50:00.665: E/AndroidRuntime(29564): Caused by: java.lang.NullPointerException
10-10 02:50:00.665: E/AndroidRuntime(29564): at com.appuccino.unitconverter.MainActivity.onCreate(MainActivity.java:37)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.Activity.performCreate(Activity.java:5206)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-10 02:50:00.665: E/AndroidRuntime(29564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
10-10 02:50:00.665: E/AndroidRuntime(29564): ... 11 more
我的 mainActivity 的第一部分:
public class MainActivity extends FragmentActivity implements
OnItemSelectedListener {
/**
* The serialization (saved instance state) Bundle key representing the
* current dropdown position.
*/
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
private Spinner firstSpinner;
private Spinner secondSpinner;
final String[] mainCategoryChoices = {"Acceleration", "Angles", "Area", "Astronomical", "Clothing", "Computers and Electronics", "Cooking", "Date/Time", "Density", "Energy"};
final String[] subCategoryChoices = {"Acre", "Barn", "Bunder", "Hundred", "Jerib", "Kappland", "Labor", "Ping", "Rai", "Shed", "Sitio", "Square", "Square Mile", "Square Yard"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR); //not necessary
setContentView(R.layout.activity_main);
ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_dropdown_item_1line,
mainCategoryChoices);
ArrayAdapter<String> secondAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_dropdown_item_1line,
subCategoryChoices);
final ActionBar actionBar = getActionBar();
if(actionBar != null)
{
actionBar.setCustomView(R.layout.action_bar_custom);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
}
firstSpinner = (Spinner) findViewById(R.id.firstSpinner);
secondSpinner = (Spinner) findViewById(R.id.secondSpinner);
firstSpinner.setAdapter(firstAdapter);
secondSpinner.setAdapter(secondAdapter);
firstSpinner.setOnItemSelectedListener(this);
secondSpinner.setOnItemSelectedListener(this);
}
日志信息指向的第 37 行转到说
final String[] subCategoryChoices = {"Acre", "Barn", "Bunder", "Hundred", "Jerib", "Kappland", "Labor", "Ping", "Rai", "Shed", "Sitio", "Square", "Square Mile", "Square Yard"};
虽然当我点击它时,它会转到 onCreate() 上方的行,是什么导致它崩溃?