在onCreate中安卓崩溃

时间:2013-10-10 14:16:52

标签: android eclipse crash nullpointerexception logcat

每当我运行我的应用程序时,第一次从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()上方的那一行,可能会让它崩溃?

3 个答案:

答案 0 :(得分:0)

我敢打赌,NPE就在这一行.-

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

根据getWindow()文档,该方法仍有可能会在null内返回onCreate。如果评论中没有必要使用该行,请尝试将其删除。

此外,您应该仔细检查activity_main布局是否包含观看firstSpinnersecondSpinner

答案 1 :(得分:0)

我最终在ssantoszookey's答案的帮助下解决了问题,我使用的是包含微调器的自定义actionBar布局,而activity_main只是用于显示片段的frameLayout,因此,我正在使用的布局上下文中不存在两个微调器。

答案 2 :(得分:-1)

似乎你在第37行空指针异常:secondSpinner = (Spinner) findViewById(R.id.secondSpinner);

Sou,您应该检查layout是否包含此Spinner