我遇到此活动的问题。
package com.ilocal.diary;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
public class ViewDiary extends Activity {
private String userid = "1";
private String response;
private String url;
private Bundle bundle = new Bundle();
private ArrayList<NameValuePair> postParameters;
private ProgressDialog dialog = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty_layout);
this.dialog = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);
bundle = this.getIntent().getExtras();
if (bundle != null) {
userid = bundle.getString("USERID");
}
url ="http://www.ilocaltest.x10.mx/android/fetchdiary.php";
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("userid", userid));
new PHPQuery().execute(url, postParameters);
}
private class PHPQuery extends AsyncTask<Object, Void, String > {
protected String doInBackground(Object... params) {
try {
response = CustomHttpClient.executeHttpPost(url, postParameters);
} catch (Exception e) {
System.out.println("HTTP error - " + e.toString());
}
System.out.println(response);
return response;
}
protected void onPostExecute(String result) {
dialog.dismiss();
System.out.println(result);
if (!(result == null)) {
bundle.putString("RESPONSE", result);
Intent i = new Intent(ViewDiary.this, DisplayDiary.class);
i.putExtras(bundle);
startActivity(i);
ViewDiary.this.finish();
}
else
finish();
}
}
}
当我在行中获得nullpointerexecption时尝试捆绑AsyncTask的结果时,一切正常工作
bundle.putString("RESPONSE", result);
我知道结果已经传递给了onPostExecute方法,所以异常来自哪里以及如何修复它。
马丁
答案 0 :(得分:1)
您实例化了bundle,但是如果意图是在没有bundle的情况下触发的话,那么你将this.getIntent().getExtras();
分配给null
,这可能就是这种情况。在放入bundle之前检查null,如果为null,则实例化它。
更好的是,检查getExtras()
之后是否为null,如果是,则实例化:
bundle = this.getIntent().getExtras();
if (bundle == null)
bundle = new Bundle();
else {//...