2011-06-12 32 views
0

我遇到此活动的问题。AsyncTask后的捆绑包中的空指针异常

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方法,以便在哪里例外来自并且如何解决它。

马丁

+0

随着'如果{'这应该是为'如果(结果相同= NULL((结果== NULL)!)! ){''''只是认为你应该知道这一点,并且'bundle'或'result'必须为空,我猜测它是结果,因为如果结果为空,它只会到达那里。 – Austin 2011-06-12 03:58:23

+0

你可以在这里放一个调试日志,看看它是否运行? bundle = this.getIntent()。getExtras(); if(bundle!= null){ userid = bundle.getString(“USERID”); Log.d(“PHP”,“Found extras”); } – 2011-06-12 03:58:27

+0

@Christopher增加了额外的功能。我已经知道,没有真正的软件包,所以创建了一个新的活动,以便发送一个包,并删除创建一个新的包,它的工作原理。它一定是空的包。但为什么这应该有所作为。在上一个创建bundle的活动中,它将一直是空的,直到将一个字符串放入其中,这与此活动相同。 – martincm 2011-06-12 04:19:25

回答

1

您实例包,但你分配给它this.getIntent().getExtras();这是null如果意图是在没有捆绑,这大概就是这样被解雇。在放入包之前检查null,如果为null,则将其实例化。

更妙的是,检查空getExtras()后实例如果这样:

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

感谢MByD,但问题是把额外的,并没有得到它们 – martincm 2011-06-12 04:25:40

+0

我试图开发它作为一个独立的活动,然后把它放到主要的应用程序,一旦它工作,所以我想我可以实例化捆绑为了测试该活动,然后将其删除后,它将被放置在主应用程序,它将从意图收到一个捆绑。将来我只会做一个虚拟活动来启动这个意图。 – martincm 2011-06-12 04:33:08