2011-06-28 105 views
2

我正在處理我的web服務,並且出現了一個奇怪的問題,我無法解決這個問題,因爲我是android開發新手。當我在異步任務的方法中顯示祝詞時,我收到空指針異常。有人請幫我解決這個問題異步任務的空指針異常

if (result.booleanValue()) // here at this line I am getting the error in logcat// 

在此先感謝....!

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ///////////male female buttons code///////////// 
    Button regmalebtn = (Button) findViewById(R.id.regmalebtn); 
    regmalebtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      gender="M"; 
      System.out.println("gender value after male button click ="+gender); 
      Log.v(TAG,"gender value on male button click ="+gender); 
     } 
    }); 
    Button regfemalebtn = (Button)findViewById(R.id.regfemalebtn); 
    regfemalebtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      gender ="F"; 
      // System.out.println("gender value on female button click ="+gender); 
      Log.v(TAG,"gender value on female button click ="+gender); 
     } 
    }); 

    ////////////male female button code will ends here///////////// 

    Button signin = (Button) findViewById(R.id.regsubmitbtn); 
    signin.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      new RegisterTask().execute(); 
     }; 
    }); 
} 


private class RegisterTask extends AsyncTask<Void, Void, Boolean> { 
    private final ProgressDialog dialog = new ProgressDialog(Register.this); 

    protected void onPreExecute() { 
     this.dialog.setMessage("Registering..."); 
     this.dialog.show(); 
    } 


    // @SuppressWarnings("unused") 
    protected Boolean doInBackground(final Void unused) { 
     return this.register(); //don't interact with the ui! 
    } 

    private Boolean register() { 
     ************some code for webservices************ 
    } 

    return null; 

} 

protected void onPostExecute(final Boolean result) { 
    if (this.dialog.isShowing()) { 
     this.dialog.dismiss(); 
    } 
    if (result.booleanValue()) { 
     // show register success dialog 
     // I m getting the error at above line 

     Toast.makeText(Register.this, "Registerd", Toast.LENGTH_SHORT).show(); 
    } 
    else { 
     Toast.makeText(Register.this, "Try Again", Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
protected Boolean doInBackground(Void... params) { 
    // TODO Auto-generated method stub 
    return register(); 
} 


} 
} 

這些r是logcat的錯誤

06-28 16:55:56.685: WARN/dalvikvm(708): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708): FATAL EXCEPTION: main 
06-28 16:55:56.695: ERROR/AndroidRuntime(708): java.lang.NullPointerException 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at com.soap.Register$RegisterTask.onPostExecute(Register.java:193) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at com.soap.Register$RegisterTask.onPostExecute(Register.java:1) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at android.os.AsyncTask.finish(AsyncTask.java:417) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at android.os.AsyncTask.access$300(AsyncTask.java:127) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at android.os.Looper.loop(Looper.java:123) 
06-28 16:55:56.695: ERROR/AndroidRuntime(708):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
+0

http://www.xoriant.com/blog/mobile-application-development/android-async-task.html – Android

+0

如何調用之前進行空校驗result.booleanValue()? – Raj

+0

我試着用下面的建議,但它只顯示進度對話框作爲輸出,沒有別的 – Raj

回答

3

你顯然從寄存器方法返回null。在調用result.booleanValue()之前,返回非空值或進行空值檢查。

你可以是這樣做的:

if (result == null) { 

} 
else if (result.booleanValue()) { 

} 
else { 

} 

順便說一句,你爲什麼要返回一個布爾值,而不是布爾(原始類型,不能爲null)。

+0

感謝您的回覆,但你可以幫助代碼片段,我很困惑 – Raj

+0

如何進行空檢查之前調用result.booleanValue()? – Raj

+0

請參閱我編輯的答案。 – kgiannakakis

0

嘗試使用爲您的吐司的activity.class名的說法。我認爲你的情況是Register.class而不是Register.this

+0

如果我使用register.class我得到的錯誤像類型Toast中的方法makeText(Context,CharSequence,int)不適用於參數(Class ,String,int) – Raj

+0

這個答案不正確。請參閱:http://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context,java.lang.CharSequence,int) – Marmoy

+0

是否註冊您的活動?我使用了很多次代碼... – Dayerman

0

嘗試將上下文傳遞給變量。或者嘗試Register.this.getApplicationContext()BaseContext。檢查對話框爲空,使在postExecute()的值傳遞,並創建新的對話框,如果不從你的要求:)

+0

謝謝,但如何檢查空值爲了不返回null,有點困惑 – Raj

+0

@Raj檢查像這樣if(dialog!= null){做剩下的操作};所以如果對話框爲空,那麼什麼都不會發生。如果該對話框不爲空,則操作會定期進行。 –

+0

謝謝,我根據logcat響應得到了prblm和我的麪包。在每一種情況下,我都會得到「再試一次」,如果(resultsRequestSOAP == null){ToS.makeText(Register.this.getApplicationContext(),「再試一次「,Toast.LENGTH_SHORT).show(); \t \t} \t \t否則,如果(resultsRequestSOAP.booleanValue()){ \t \t //也顯示註冊成功的對話框 \t \t \t Toast.makeText(Register.this.getApplicationContext(), 「Registerd」,吐司。 LENGTH_SHORT).show(); \t \t} \t \t否則{ \t \t \t Toast.makeText(Register.this.getBaseContext(), 「字段爲空」,Toast.LENGTH_SHORT).show(); \t \t} – Raj

0

我寫這

protected void onPostExecute(String unused) { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 

      Toast.makeText(
        myclassname.this, 
        "Downloading of " + FILENAME 
          + " complete.", Toast.LENGTH_LONG).show(); 
      refreshList(); 


     } 

重疊,這是workning爲我..

see this link for more detail
http://hi.baidu.com/hi_android/blog/item/fea149f90da7fdedfd037f56.html
http://www.screaming-penguin.com/node/7746

+0

@Pragana,謝謝,我根據logcat的response.In餘米越來越 「重試」 如果(resultsRequestSOAP == NULL)每一個案件{ \t \t \t Toast.makeText得到了與我舉杯prblm(寄存器。 this.getApplicationContext(),「再試一次」,Toast.LENGTH_SHORT).show(); \t \t} \t \t否則,如果(resultsRequestSOAP.booleanValue()){ \t \t //也顯示註冊成功的對話框 \t \t \t Toast.makeText(Register.this.getApplicationContext(), 「Registerd」,吐司。 LENGTH_SHORT).show(); \t \t} \t \t否則{ \t \t \t Toast.makeText(Register.this.getBaseContext(), 「字段爲空」,Toast.LENGTH_SHORT).show(); \t \t} – Raj

0

我被推出的TOA擺脫相同的吐司/異步/ Nullpointe問題TS是這樣的:

Toast.makeText(getApplicationContext(), "Hello Toast!", Toast.LENGTH_SHORT).show();