2017-01-16 17 views
-2

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)  
+0

私は今、投稿の答えをチェックし@Peter。 – W4R10CK

答えて

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) { 

    } 
});