-1

每当我运行我的应用程序时,我第一次从 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() 上方的行,是什么导致它崩溃?

4

3 回答 3

0

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

于 2013-10-10T17:21:54.380 回答
0

我敢打赌,NPE在这一行。-

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

根据getWindow()文档,该方法仍有可能返回nullinside onCreate。如果评论指出该行不是必需的,请尝试将其删除。

此外,您应该仔细检查您的activity_main布局是否包含视图firstSpinnersecondSpinner.

于 2013-10-10T14:24:31.597 回答
-1

看来您在第 37 行出现空指针异常: secondSpinner = (Spinner) findViewById(R.id.secondSpinner);

苏,你应该检查你的layout包含这个Spinner

于 2013-10-10T14:45:16.620 回答