AsyncTask 之后捆绑中的 NullPointerExecption

发布于 2024-11-14 19:40:43 字数 2374 浏览 3 评论 0原文

我在执行此活动时遇到问题。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2024-11-21 19:40:43

您实例化捆绑包,但随后分配给它 this.getIntent().getExtras(); 如果意图是在没有捆绑包的情况下触发的,则为 null,这可能是案件。在放入包之前检查是否为 null,如果为 null,则实例化它。

更好的是,检查 getExtras() 之后是否为 null,如果是则实例化:

bundle = this.getIntent().getExtras();
if (bundle == null)
    bundle = new Bundle();
else {//...

You instantiate bundle, but then you assign to it this.getIntent().getExtras(); which is null 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:

bundle = this.getIntent().getExtras();
if (bundle == null)
    bundle = new Bundle();
else {//...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文