0

当我运行我的应用程序并从我的微调器中选择一个项目而不是像应用程序崩溃那样打开一个新类时遇到问题,我想知道是否有人可以帮助我解决为什么会发生这种情况。我不确定要包含代码的哪一部分,所以我将包含 Logcat,如果您请求任何代码,我将编辑这篇文章。

谢谢。

日志猫:

03-09 15:36:53.814: E/AndroidRuntime(13429): FATAL EXCEPTION: main
03-09 15:36:53.814: E/AndroidRuntime(13429): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.famouspeople/com.example.famouspeople.SeanConnery}: java.lang.NullPointerException
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.os.Looper.loop(Looper.java:137)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at java.lang.reflect.Method.invokeNative(Native Method)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at java.lang.reflect.Method.invoke(Method.java:511)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at dalvik.system.NativeStart.main(Native Method)
03-09 15:36:53.814: E/AndroidRuntime(13429): Caused by: java.lang.NullPointerException
03-09 15:36:53.814: E/AndroidRuntime(13429):    at com.example.famouspeople.SeanConnery.initialize(SeanConnery.java:44)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at com.example.famouspeople.SeanConnery.onCreate(SeanConnery.java:28)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.Activity.performCreate(Activity.java:5206)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-09 15:36:53.814: E/AndroidRuntime(13429):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-09 15:36:53.814: E/AndroidRuntime(13429):    ... 11 more
4

1 回答 1

2

通常,当应用程序因单击微调器而崩溃时,问题出在您用于填充微调器的适配器上,请仔细检查您定义的适配器长度。遵循此代码

 int num = SpinnerData.length;
    SpinnerData = new String[num];
Log.d("ArrayAdapter",""+ SpinnerData.length);
                    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(CurrentActivity.this, android.R.layout.simple_list_item_1, SpinnerData);
                    //spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinner.setAdapter(spinnerArrayAdapter);

仔细检查长度..我希望这有帮助

于 2013-03-11T12:37:03.873 回答