我希望我的微调器从一个从php mysql中检索的字符串中设置选择()。
private class RetrievePersonal extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(VirtualIC.this);
pDialog.setMessage("Loading personal details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("accountID", accountID));
JSONObject json = jParser.makeHttpRequest(url_personal_details, "GET", params);
Log.d("Personal Details>>", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray personalObj = json.getJSONArray(TAG_PERSONAL);
JSONObject prsl = personalObj.getJSONObject(0);
FindViewById();
name = prsl.getString("name");
nric = prsl.getString("nric");
nationality = prsl.getString("nationality");
religion = prsl.getString("religion");
races = prsl.getString("races");
gender = prsl.getString("nationality");
dob = prsl.getString("dob");
email = prsl.getString("email");
mobile = prsl.getString("mobileNum");
home = prsl.getString("homeNum");
address = prsl.getString("address");
postcode = prsl.getString("postcode");
city = prsl.getString("city");
state = prsl.getString("state");
country = prsl.getString("country");
}else{
}
} catch (Exception e) {
e.printStackTrace();
e.toString();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
FindViewById();
inputName.setText(name);
inputNric.setText(nric);
inputNationality.setText(nationality);
inputReligion.setText(nationality);
inputRaces.setText(nationality);
if(gender.equals("M")){
inputGender.setSelection(getIndex(inputGender, "Male"));
}else{
inputGender.setSelection(getIndex(inputGender, "Female"));
}
inputDay.setSelection(getIndex(inputDay, day));
inputMonth.setSelection(getIndex(inputMonth, month));
inputYear.setSelection(getIndex(inputYear, year));
inputEmail.setText(email);
inputMobileNum.setText(mobile);
inputHomeNum.setText(home);
inputAddress.setText(address);
inputPostcode.setText(postcode);
inputCity.setText(city);
inputState.setSelection(getIndex(inputDay, state));
if(getIndex(inputCountry, country) > 0) {
inputCountry.setSelection(getIndex(inputCountry, country));
}else{
inputCountry.setSelection(getIndex(inputCountry, "Others"));
countryOtherLayout.setVisibility(View.VISIBLE);
inputCountryOther.setText(country);
}
}
}
getIndex()返回位置......
private int getIndex(Spinner spinner, String string){
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(string)) {
index = i;
break;
}else{
index = -1;
break;
}
}
return index;
}
这就是我得到的.....
java.lang.NullPointerException:尝试调用虚方法&#39; int android.widget.Spinner.getCount()&#39;在空对象引用上
请帮帮忙?谢谢。
private void FindViewById(){
inputGender = (Spinner) findViewById(R.id.gender);
inputMonth = (Spinner) findViewById(R.id.month);
inputState = (Spinner) findViewById(R.id.state);
inputName = (EditText) findViewById(R.id.name);
inputNric = (EditText) findViewById(R.id.nric);
inputNationality = (EditText) findViewById(R.id.nationality);
inputReligion = (EditText) findViewById(R.id.religion);
inputRaces = (EditText) findViewById(R.id.races);
inputEmail = (EditText) findViewById(R.id.email);
inputMobileNum = (EditText) findViewById(R.id.contactnum_hp);
inputHomeNum = (EditText) findViewById(R.id.contactnum_home);
inputAddress = (EditText) findViewById(R.id.address);
inputPostcode = (EditText) findViewById(R.id.postcode);
inputCity = (EditText) findViewById(R.id.city);
inputCountryOther = (EditText) findViewById(R.id.countryOther);
countryLayout = (LinearLayout) findViewById(R.id.countryLayout);
nameLayout = (TextInputLayout) findViewById(R.id.nameLayout);
nricLayout = (TextInputLayout) findViewById(R.id.nricLayout);
nationalityLayout = (TextInputLayout) findViewById(R.id.nationalityLayout);
religionLayout = (TextInputLayout) findViewById(R.id.religionLayout);
racesLayout = (TextInputLayout) findViewById(R.id.racesLayout);
emailLayout = (TextInputLayout) findViewById(R.id.emailLayout);
hpLayout = (TextInputLayout) findViewById(R.id.contactHPLayout);
homeLayout = (TextInputLayout) findViewById(R.id.contactHomeLayout);
addressLayout = (TextInputLayout) findViewById(R.id.addressLayout);
postcodeLayout = (TextInputLayout) findViewById(R.id.postcodeLayout);
cityLayout = (TextInputLayout) findViewById(R.id.cityLayout);
countryOtherLayout = (TextInputLayout) findViewById(R.id.countryOtherLayout);
}