我的應用程序有幾個EditText框,當按下「完成」按鈕時,我將驗證輸入並返回活動結果。問題是,當我試圖讀取數據時它是空的,我不明白爲什麼。爲什麼我會得到空指針異常,試圖訪問EditText數據?
這裏是我的代碼:
public class CreatePlayer extends Activity implements OnItemSelectedListener {
private EditText textFirstName;
private EditText textLastName;
private EditText jerseyNumber;
private Spinner spinner;
private int arrayID;
private boolean validFirstName = false;
private boolean validLastName = false;
private boolean validJerseyNumber = false;
private boolean validPosition = false;
private static final String TAG = "CreatePlayer";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createplayer);
final EditText textFirstName = (EditText) findViewById(R.id.playername_first_edit);
EditText textLastName = (EditText) findViewById(R.id.playername_last_edit);
final EditText jerseyNumber = (EditText) findViewById(R.id.playernum_edit);
arrayID = getIntent().getExtras().getInt("arrayID");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, arrayID, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
textFirstName
.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
boolean result = v
.getText()
.toString()
.matches(
getString(R.string.regex_first_name));
System.out.println(v.getText());
System.out.println(result);
Pattern p = Pattern
.compile(getString(R.string.regex_first_name));
Matcher m = p.matcher(v.getText());
Log.v(TAG, "pattern=" + p.toString());
Log.v(TAG, "text=" + v.getText());
Log.v(TAG, "result=" + result);
Log.v(TAG, "find=" + m.find());
if (result) {
validFirstName = true;
//textFirstName = (EditText) v.getText();
}
}
return false;
}
});
textLastName
.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
boolean result = v
.getText()
.toString()
.matches(
getString(R.string.regex_last_name));
Pattern p = Pattern
.compile(getString(R.string.regex_last_name));
Matcher m = p.matcher(v.getText());
Log.v(TAG, "pattern=" + p.toString());
Log.v(TAG, "text=" + v.getText());
Log.v(TAG, "result=" + result);
Log.v(TAG, "find=" + m.find());
if (result) {
validLastName = true;
}
}
return false;
}
});
jerseyNumber
.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Pattern p = Pattern
.compile(getString(R.string.regex_jerseynum));
boolean result = v
.getText()
.toString()
.matches(
getString(R.string.regex_jerseynum));
Matcher m = p.matcher(v.getText());
Log.v(TAG, "pattern=" + p.toString());
Log.v(TAG, "text=" + v.getText());
Log.v(TAG, "result=" + result);
Log.v(TAG, "find=" + m.find());
if (result) {
validJerseyNumber = true;
}
}
return false;
}
});
}
public void okClicked(View v) {
PlayerInfo newPlayer;
if (validFirstName && validLastName && validJerseyNumber
&& validPosition) {
Log.v(TAG, "JerseyNumber = " + jerseyNumber);
// jerseyNumber is null???
int num = Integer.parseInt(jerseyNumber.getText().toString());
String type = spinner.getSelectedItem().toString();
Log.v(TAG, "type = " + type);
String name = textFirstName.getText().toString() + " "
+ textLastName.getText().toString();
newPlayer = new PlayerInfo(BaseBoard.playerIDCounter, num, type,
name);
Intent result = new Intent();
result.putExtra(getString(R.string.players_create), newPlayer);
setResult(5, result);
this.finish();
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"Invalid input. Try something like John Smith, #27, PG",
Toast.LENGTH_SHORT);
toast.show();
}
}
public void cancelClicked(View v) {
this.setResult(-1, null);
this.finish();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
validPosition = true;
System.out.println(parent.getItemAtPosition(pos).toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
你知道哪些代碼行異常發生? –
更新。如果我將其更改爲jerseyNumber =(EditText)findViewById(R.id.playernum_edit);來自EditText jerseyNumber =(EditText)findViewById(R.id.playernum_edit);我得到JerseyNumber = [email protected],但它仍然給出一個空指針異常。 –
導致異常的行是int num = Integer.parseInt(jerseyNumber.getText()。toString());顯然jerseyNumber無效。我想知道是否IME_ACTION_DONE正在銷燬jerseyNumber的值,並且應該將其更改爲IME_ACTION_NEXT。 –