gpt4 book ai didi

java - 异步任务中的对话框错误

转载 作者:行者123 更新时间:2023-11-30 04:30:33 32 4
gpt4 key购买 nike

当我尝试显示异步任务的工作对话框时,出现以下错误:java.lang.reflect.InvocationTargetException异常java.lang.NullPointerException

activity 和 asynctask 的代码是:

public class MainActivity extends Activity {

private ControlLogin ctlLogin;
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

ctlLogin = (ControlLogin)findViewById(R.id.controlLogin);

ctlLogin.setOnLoginListener(new OnLoginListener()
{
@Override
public void onLogin(String email, String password, Boolean saveAccount){


ProgressDialog dialog = new ProgressDialog(getApplicationContext());
dialog.setMessage("Signing...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);

new Login().execute(email, password);
}
});



}

public class Login extends AsyncTask<String, Float, CloudApp> {

protected void onPreExecute(){
dialog.show();
}
@Override
protected CloudApp doInBackground(String... arg0) {
CloudApp api = new CloudAppImpl(arg0[0], arg0[1]);
return api;
}

protected void onPostExecute(CloudApp api){

dialog.dismiss();
try {
CloudAppAccount acc = api.getAccountDetails();
Toast toast = Toast.makeText(getBaseContext(), "test: " + acc.getEmail(), Toast.LENGTH_LONG);
toast.show();
} catch (CloudAppException e) {
e.printStackTrace();
}
}

}

}

有什么帮助吗???谢谢!!

最佳答案

您通过在 onLogin(...) 方法中定义局部变量 dialog 来隐藏您的代码中的成员 dialog。因此,dialog 永远不会被初始化为任何东西,这就是为什么您在 Login 类中获得 NPE 的原因。

    @Override
public void onLogin(String email, String password, Boolean saveAccount){

//remove the leading ProgessDialog here...it is hiding your member 'dialog'
//dialog = new ProgressDialog(getApplicationContext());
ProgressDialog dialog = new ProgressDialog(getApplicationContext());
dialog.setMessage("Signing...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);

new Login().execute(email, password);
}

关于java - 异步任务中的对话框错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812047/

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