尝试在空对象引用上调用虚拟方法'void android.widget.Spinner.setSelection(int)'

时间:2018-07-05 05:58:41

标签: java android xml android-layout android-spinner

您好,我是Android开发的初学者。 我在MainActivity中遇到了空指针异常。 (我知道有关此主题的问题很多,但没有任何具体帮助我的东西)

> 07-05 05:23:00.407 3229-3229/package.name E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my.package.name, PID: 3229
    java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.name/my.package.name.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setSelection(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setSelection(int)' on a null object reference
        at graphicstool.battlegrounnd.gfx.MainActivity.revertConfig(MainActivity.java:235)
        at graphicstool.battlegrounnd.gfx.MainActivity.onCreate(MainActivity.java:377)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

请参阅上述错误。 这是我的特定错误代码

private void revertConfig() {
        SharedPreferences sharedPreferences = getSharedPreferences(getPackageName(), 0);
        this.sprGraphicsQuality.setSelection(sharedPreferences.getInt("sprGraphicsQuality", 0));

    }
protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        verifyStoragePermissions(this);
        Spinner sprGraphicsQuality =(Spinner) findViewById(R.id.sprGraphicsQuality);

            ArrayAdapter adapter = createFromResource(this, R.array.qualitysetting, R.layout.activity_main);

            adapter.setDropDownViewResource(R.layout.activity_main);

            sprGraphicsQuality.setAdapter(adapter);
            revertConfig();

在这件事上请帮助我。 让我知道您是否需要更多详细信息。

0 个答案:

没有答案