gpt4 book ai didi

android - 异步任务的空指针异常

转载 作者:搜寻专家 更新时间:2023-11-01 09:13:27 24 4
gpt4 key购买 nike

我正在处理我的 web 服务,我遇到了一个奇怪的问题,我无法解决这个问题,因为我是 android 开发的新手。在异步任务的 onPostExecute() 方法中显示 toast 时出现空指针异常。有人请帮我解决这个问题

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();
}


}
}

这些是 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)

最佳答案

您显然从 register 方法返回 null。在调用 result.booleanValue() 之前返回不为 null 的内容或进行 null 检查。

你可以这样做:

if (result == null) {

}
else if (result.booleanValue()) {

}
else {

}

对了,为什么要返回一个Boolean,而不是boolean(原始类型,不能为null)。

关于android - 异步任务的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com