我是新手,请多多包涵。我创建了一个微调器并用一个字符串数组填充它,然后我创建了一个方法“OnItemSelected”并插入了一个 switch 语句。然后该活动抛出 NullPointerException。当我删除 switch 语句时,该活动有效。我怎样才能解决这个问题?
代码:
EditText player1 = null;
EditText player2 = null;
EditText player3 = null;
EditText player4 = null;
EditText player5 = null;
EditText player6 = null;
EditText player7 = null;
EditText player8 = null;
Spinner spinner;
int numberOfPlayers = 0;
int counter = 0;
private String[] players ={"2","3","4", "5", "6", "7", "8"};
//private String[] names ={" "};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
spinner = (Spinner)findViewById(R.id.spinnerplayers);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, players);
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.findViewById(R.id.editTextPlayer1);
player2.findViewById(R.id.editTextPlayer2);
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
break;
case 1:
player1.findViewById(R.id.editTextPlayer1);
player2.findViewById(R.id.editTextPlayer2);
player3.findViewById(R.id.editTextPlayer3);
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
break;
case 2:
player1.findViewById(R.id.editTextPlayer1);
player2.findViewById(R.id.editTextPlayer2);
player3.findViewById(R.id.editTextPlayer3);
player4.findViewById(R.id.editTextPlayer4);
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
break;
case 3:
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);
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.VISIBLE);
player3.setVisibility(View.VISIBLE);
player4.setVisibility(View.VISIBLE);
player5.setVisibility(View.VISIBLE);
break;
case 4:
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);
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.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);
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.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);
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;
}*/
}
然后这是我的日志:
06-06 12:44:42.433: E/AndroidRuntime(5764): FATAL EXCEPTION: main
06-06 12:44:42.433: E/AndroidRuntime(5764): java.lang.NullPointerException
06-06 12:44:42.433: E/AndroidRuntime(5764): at com.brightdesign.truthordare.Main.onItemSelected(Main.java:69)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.widget.AdapterView.fireOnSelected(AdapterView.java:873)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.widget.AdapterView.access$300(AdapterView.java:45)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:839)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.os.Handler.handleCallback(Handler.java:587)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.os.Looper.loop(Looper.java:123)
06-06 12:44:42.433: E/AndroidRuntime(5764): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 12:44:42.433: E/AndroidRuntime(5764): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 12:44:42.433: E/AndroidRuntime(5764): at java.lang.reflect.Method.invoke(Method.java:521)
06-06 12:44:42.433: E/AndroidRuntime(5764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 12:44:42.433: E/AndroidRuntime(5764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 12:44:42.433: E/AndroidRuntime(5764): at dalvik.system.NativeStart.main(Native Method)
提前致谢!