2011-06-12 4 views
0

이 활동에 문제가 있습니다. 내가AsyncTask 이후 번들의 NullPointerExecption

bundle.putString("RESPONSE", result); 

나는 여기서 결과가 그렇게 onPostExecute 메서드에 전달 된 것을 알고 선에 nullpointerexecption을 얻을 때 내가 번들에게 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(); 
     } 
    } 
} 

모든 노력 예외는 어떻게 고칠 수 있는가?

마틴

+0

) {'그냥 당신이 그것을 알아야한다고 생각하고,'bundle' 또는'result' 중 하나가 null이되어야합니다. 결과가 null 인 경우에만 거기에 도착하기 때문에 결과를 추측합니다. – Austin

+0

여기에 디버그 로그를 넣고 실행되는지 확인할 수 있습니까? 번들 = this.getIntent(). getExtras(); if (번들! = null) { userid = bundle.getString ("USERID"); Log.d ("PHP", "Found extras"); } –

+0

@Christopher 추가 된 내용과 추가 내용이 있습니다. 이미 실제 번들이 없다는 것을 이미 알고 있었기 때문에 번들을 보내고 새로운 번들을 만드는 것을 제거한 새로운 활동이 inorder를 만들었고 작동합니다. null 인 번들이어야합니다. 그러나 그것이 왜 달라져야 하는가? 번들이 생성 된 이전 활동에서는이 활동과 동일한 문자열이 입력 될 때까지 비어있게됩니다. – martincm

답변

1

당신은 번들의 인스턴스를,하지만 당신은 그것을 의도는 아마도 경우가없는 번들로 해고 된 경우 null이다 this.getIntent().getExtras();에 할당합니다. 번들에 넣기 전에 null을 확인하고, null이면 인스턴스화하십시오.

가 더 좋은 널 경우 getExtras() 후 확인 인스턴스화 그렇다면 (! (결과 == NULL의))! '의 경우 {'즉, (만약 있다면 발생할 '과 같아야 = null를

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

고마워요 MByD하지만 문제가 여분을 가하고 그들을지고 있지 않다 – martincm

+0

내가 그것을 독립 실행 형 활동으로 개발하고 그것을 주 애플 리케이션에 넣어 한번 노력하고 있었으므로, 그래서 내가 테스트하기 위해 번들을 인스턴스 수 있다고 생각 액티비티를 제거한 다음 기본 앱에 배치 한 후 제거하여 인 텐트에서 번들을 수신합니다. 미래에 나는 의도를 시작하기위한 거짓 행동 만 할 것이다. – martincm