初心者なのでお手柔らかにお願いします。Edit Texts の可視性を制御するスピナーを備えたアプリを作成しました。このアプリケーションを実行すると、catch ステートメントが null ポイントの例外をキャッチし、スピナーに文字列配列が取り込まれません。アイデアや助けはありますか?
これが私のコードです
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
player1.findViewById(R.id.editTextPlayer1);
player2.findViewById(R.id.editTextPlayer2);
player3.findViewById(R.id.editTextPlayer3);
player4.findViewById(R.id.editTextPlayer4);
player5.findViewById(R.id.editTextPlayer5);
player6.findViewById(R.id.editTextPlayer6);
player7.findViewById(R.id.editTextPlayer7);
player8.findViewById(R.id.editTextPlayer8);
Spinner spinner = (Spinner)findViewById(R.id.spinnerplayers);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.peoplearray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}catch (Exception e) {
// handle any errors
Log.e("TestActivity", "Error in activity", e); // log the error
// Also let the user know something went wrong
Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long id) {
numberOfPlayers = pos + 2;
switch(pos){
case 0:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
break;
case 1:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
break;
case 2:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
break;
case 3:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
player5.setVisibility(View.VISIBLE);
break;
case 4:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
player5.setVisibility(View.VISIBLE);
player6.setVisibility(View.VISIBLE);
break;
case 5:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
player5.setVisibility(View.VISIBLE);
player6.setVisibility(View.VISIBLE);
player7.setVisibility(View.VISIBLE);
break;
case 6:
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
player5.setVisibility(View.VISIBLE);
player6.setVisibility(View.VISIBLE);
player7.setVisibility(View.VISIBLE);
player8.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
logcat エラーは次のとおりです。
06-05 15:10:03.986: E/TestActivity(1815): Error in activity
06-05 15:10:03.986: E/TestActivity(1815): java.lang.NullPointerException
06-05 15:10:03.986: E/TestActivity(1815): at com.brightdesign.truthordare.Main.onCreate(Main.java:42)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.Activity.performCreate(Activity.java:4465)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-05 15:10:03.986: E/TestActivity(1815): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 15:10:03.986: E/TestActivity(1815): at android.os.Looper.loop(Looper.java:137)
06-05 15:10:03.986: E/TestActivity(1815): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-05 15:10:03.986: E/TestActivity(1815): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 15:10:03.986: E/TestActivity(1815): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 15:10:03.986: E/TestActivity(1815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-05 15:10:03.986: E/TestActivity(1815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-05 15:10:03.986: E/TestActivity(1815): at dalvik.system.NativeStart.main(Native Method)