0

我在使用微调器和字符串数组时遇到问题。我想根据条件更改微调器项目(我的术语中的模式)

以下是我所做的代码:

public class MainActivity extends Activity {

    .
    .
    .

    ArrayAdapter<CharSequence> aaParam1;
    ArrayAdapter<CharSequence> aaParam2;
    ArrayAdapter<CharSequence> aaParam3;   
    .
    .
    .

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    .
            .
            .

    ReSpin(1);
            .
            .
            .
    }

    public void ReSpin(int mode){
            int stepval = Integer.parseInt(setting.getString("steps", "1")); 

            switch(mode){
            case 1:
                aaParam1 = ArrayAdapter.createFromResource(this, R.array.mode1param1, android.R.layout.simple_spinner_dropdown_item);
                aaParam2 = ArrayAdapter.createFromResource(this, R.array.mode1param2, android.R.layout.simple_spinner_dropdown_item);
                aaParam3 = ArrayAdapter.createFromResource(this, R.array.mode1param3, android.R.layout.simple_spinner_dropdown_item);
                break;
            case 2:
                aaParam1 = ArrayAdapter.createFromResource(this, R.array.mode2param1, android.R.layout.simple_spinner_dropdown_item);
                aaParam2 = ArrayAdapter.createFromResource(this, R.array.mode2param2, android.R.layout.simple_spinner_dropdown_item);
                aaParam3 = ArrayAdapter.createFromResource(this, R.array.mode2param3, android.R.layout.simple_spinner_dropdown_item);
                break;
            case 3:
                aaParam1 = ArrayAdapter.createFromResource(this, R.array.mode3param1, android.R.layout.simple_spinner_dropdown_item);
                aaParam2 = ArrayAdapter.createFromResource(this, R.array.mode3param2, android.R.layout.simple_spinner_dropdown_item);
                aaParam3 = ArrayAdapter.createFromResource(this, R.array.mode3param3, android.R.layout.simple_spinner_dropdown_item);
                break;
            }

            aaParam1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            aaParam2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            aaParam3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spinParam1.setAdapter(aaParam1);
            spinParam2.setAdapter(aaParam2);
            spinParam3.setAdapter(aaParam3);

    }

}

当从 OnCreate() 调用 ReSpin() 函数时,上述代码总是崩溃/退出

日志:

01-05 17:02:04.151: W/dalvikvm(22040): threadid=1: thread exiting with uncaught exception (group=0x41cc0930)
01-05 17:02:04.183: E/AndroidRuntime(22040): FATAL EXCEPTION: main
01-05 17:02:04.183: E/AndroidRuntime(22040): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micom.testspin/com.micom.testspin.MainActivity}: java.lang.NullPointerException
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.os.Looper.loop(Looper.java:137)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at java.lang.reflect.Method.invoke(Method.java:511)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at dalvik.system.NativeStart.main(Native Method)
01-05 17:02:04.183: E/AndroidRuntime(22040): Caused by: java.lang.NullPointerException
01-05 17:02:04.183: E/AndroidRuntime(22040):    at com.micom.testspin.MainActivity.ReSpin(MainActivity.java:273)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at com.micom.testspin.MainActivity.onCreate(MainActivity.java:67)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.Activity.performCreate(Activity.java:5104)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-05 17:02:04.183: E/AndroidRuntime(22040):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-05 17:02:04.183: E/AndroidRuntime(22040):    ... 11 more
01-05 17:02:04.206: W/ActivityManager(390):   Force finishing activity com.micom.testspin/.MainActivity
4

1 回答 1

0

根据堆栈跟踪,您在 MainActivity.java 的第 273 行有一个空指针异常。看看那里,看看哪个变量可能没有被初始化。

于 2013-01-06T03:53:43.873 回答