我正在處理我的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)
http://www.xoriant.com/blog/mobile-application-development/android-async-task.html – Android
如何調用之前進行空校驗result.booleanValue()? – Raj
我試着用下面的建議,但它只顯示進度對話框作爲輸出,沒有別的 – Raj