Android Studio“尝试在空对象引用上调用虚方法”

时间:2017-01-16 17:11:15

标签: android android-studio nullpointerexception

我希望显示1到3之间的数字,具体取决于您在两个微调器中选择的对象。一切都按预期工作,直到我使用以下代码行导致错误。

int pos1 = spinnerW.getSelectedItemPosition();
int pos2 = spinnerS.getSelectedItemPosition();
Toast.makeText(getBaseContext(),ergebnis(pos1, pos2),Toast.LENGTH_LONG);

是否有其他方法可以获得所选对象的位置? 提前致谢。 完整代码:

package com.example.peter.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends AppCompatActivity {
    public static int ergebnis (int pos1, int pos2){
    int[][] array = new int[25][25];
    array[0][0] = 1;
    array[0][1] = 1;
    array[0][2] = 1;
    array[0][3] = 2;
    array[0][4] = 3;
    array[1][0] = 1;
    array[1][1] = 1;
    array[1][2] = 1;
    array[1][3] = 2;
    array[1][4] = 3;
    array[2][0] = 1;
    array[2][1] = 1;
    array[2][2] = 2;
    array[2][3] = 2;
    array[2][4] = 3;
    array[3][0] = 1;
    array[3][1] = 2;
    array[3][2] = 2;
    array[3][3] = 2;
    array[3][4] = 3;
    array[4][0] = 2;
    array[4][1] = 2;
    array[4][2] = 3;
    array[4][3] = 3;
    array[4][4] = 3;
    return array[pos1][pos2];
}

Spinner spinnerW;
ArrayAdapter<CharSequence> adapterW;
Spinner spinnerS;
ArrayAdapter<CharSequence> adapterS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spinnerW = (Spinner) findViewById(R.id.wahrscheinlichkeit);
    adapterW = ArrayAdapter.createFromResource(this, R.array.wahrscheinlichkeit, android.R.layout.simple_spinner_item);
    adapterW.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerW.setAdapter(adapterW);

    spinnerS.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " ausgewählt", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    spinnerS = (Spinner)findViewById(R.id.schadensausmaß);
    adapterS = ArrayAdapter.createFromResource(this,R.array.schadensausmaß,android.R.layout.simple_spinner_item);
    adapterS.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerS.setAdapter(adapterS);
    spinnerS.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    int pos1 = spinnerW.getSelectedItemPosition();
    int pos2 = spinnerS.getSelectedItemPosition();

    Toast.makeText(getBaseContext(),ergebnis(1, 2),Toast.LENGTH_LONG);
}


}

完整的错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.peter.myapplication, PID: 25679
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.peter.myapplication/com.example.peter.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
                  at android.app.ActivityThread.access$900(ActivityThread.java:154)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5490)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
                  at com.example.peter.myapplication.MainActivity.onCreate(MainActivity.java:58)
                  at android.app.Activity.performCreate(Activity.java:6285)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5490) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:1)

改变这个:

spinnerS.setOnItemSelectedListener(new OnItemSelectedListener() {......

有了这个:

spinnerW = (Spinner) findViewById(R.id.wahrscheinlichkeit);
adapterW = ArrayAdapter.createFromResource(this, R.array.wahrscheinlichkeit, android.R.layout.simple_spinner_item);
adapterW.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerW.setAdapter(adapterW);

//.....here inplace of `S` put `W`
spinnerW.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " ausgewählt", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});