AsyncTask 之后捆绑中的 NullPointerExecption
我在执行此活动时遇到问题。
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();
}
}
}
一切正常,直到我尝试将 AsyncTask 的结果捆绑在一起,当我在该行中收到 nullpointerexecption 时,
bundle.putString("RESPONSE", result);
我知道结果已传递给 onPostExecute 方法,那么异常来自哪里以及如何修复它。
马丁
I'm having a problem with this Activity.
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();
}
}
}
Everything is working until i try the bundle the result from the AsyncTask when I get a nullpointerexecption in the line
bundle.putString("RESPONSE", result);
I know that the result has been passed to the onPostExecute method so where is the exception coming from and how can fix it.
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实例化捆绑包,但随后分配给它
this.getIntent().getExtras();
如果意图是在没有捆绑包的情况下触发的,则为null
,这可能是案件。在放入包之前检查是否为 null,如果为 null,则实例化它。更好的是,检查
getExtras()
之后是否为 null,如果是则实例化:You instantiate bundle, but then you assign to it
this.getIntent().getExtras();
which isnull
if the intent was fired with no bundle, which is probably the case. check for null before putting into the bundle, and if null, instantiate it.Even better, check if null after
getExtras()
and instantiate if so: