我正在處理我的Web服務,並且遇到了一個奇怪的問題。我無法解決這個問題,因爲我是android開發新手。我在異步任務的onPostExecute()方法中收到空指針異常。當我將結果與字符串進行比較時發生這種情況。AsyncTask中的onPostExecute中的NullPointer異常
if (result.contains("Duplicate")) or
if (result.equals("Duplicate")) or
if (result.equalsIgnoreCase("Duplicate")) or
所有三個給出錯誤。我通過System.out.println(result)打印了結果的值;它顯示null
爲了讓我的問題更加怪異,這種情況只有在我的老闆測試時纔會發生。其他時候它的功能非常好。設備和網絡都保持不變。此前它曾經只發生在仿真器上。我曾經重新啓動eclipse,並且問題用於解決問題。
請幫我一把!
代碼
private void startDownload() {
new AddTask().execute(FILENAME);
}
public class AddTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(ContactDetails.this, "Loading",
"Please Wait...");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result.contains("Duplicate")) { //---- here error comes
int ecolor = -16777216; // whatever color you want
String estring = "Account with this email already exists!!";
ForegroundColorSpan fgcspan = new ForegroundColorSpan(
ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(
estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
et2.setError(ssbuilder);
dialog.dismiss();
} else {
Toast.makeText(ContactDetails.this, "Account Created!",
Toast.LENGTH_LONG).show();
dialog.dismiss();
myDialog = new Dialog(ContactDetails.this);
myDialog.setContentView(R.layout.email_alert);
myDialog.setTitle("Confirmation");
myDialog.setCancelable(true);
// for OK
Button ok = (Button) myDialog.findViewById(R.id.alertOk);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ContactDetails.this,
Login.class);
startActivity(intent);
}
});
myDialog.show();
}
}
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
is = null;
individual();
return is;
}
public void individual() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://website.com/contact.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
10);
nameValuePairs.add(new BasicNameValuePair("Name", str1));
nameValuePairs.add(new BasicNameValuePair("Email", str2));
nameValuePairs.add(new BasicNameValuePair("Password", str3));
nameValuePairs.add(new BasicNameValuePair("Address", str5));
nameValuePairs.add(new BasicNameValuePair("Pin", str11));
nameValuePairs.add(new BasicNameValuePair("City", spt1));
nameValuePairs.add(new BasicNameValuePair("State", str12));
nameValuePairs.add(new BasicNameValuePair("phone_visibility",
str_radio));
nameValuePairs.add(new BasicNameValuePair("No", str7));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = EntityUtils.toString(entity);
JSONArray jArray = new JSONArray(is);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
---編輯---
什麼可以得到一個空值作爲結果的原因是什麼?這是網絡問題嗎? 有沒有一種方法可以確保我不會得到空結果? 我的應用程序將無法正常工作,如果我得到空
你爲什麼不檢查NPE? –
是的...驗證結果字符串在onPostExecute –