-2
我希望顯示1到3之間的數字,具體取決於您在兩個旋鈕中選擇的對象。一切都按照預期工作,直到我使用了以下代碼行,導致出錯。Android Studio「嘗試在空對象引用上調用虛擬方法」
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)
@Peter檢查我現在公佈答案。 – W4R10CK