asynctask中的对话框错误
当我尝试显示异步任务的工作对话框时,出现以下错误: java.lang.reflect.InitationTargetException java.lang.NullPointerException
活动和异步任务的代码是:
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();
}
}
}
}
有帮助吗???谢谢!!
When I try to show a working dialog for an asynctask i get the following errors:
java.lang.reflect.InvocationTargetException
java.lang.NullPointerException
The code of the activity and asynctask is:
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();
}
}
}
}
Any help??? Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在
onLogin(...)
方法中定义局部变量dialog
来隐藏代码中的成员dialog
。因此,dialog
永远不会初始化为任何内容,这就是您在Login
类中获得 NPE 的原因。You are hiding your member
dialog
in your code by defining a local variabledialog
in youronLogin(...)
method. Because of this,dialog
is never initialized to anything and that is why you get an NPE in yourLogin
class.当您关闭对话框时,您确定对话框存在吗?我认为在 onLogin 中,您重新创建了一个与活动字段变量同名的新对话框变量。
你可以谨慎行事:
Are you sure your dialog exist when you dismiss it? I think in the onLogin you recreate a new dialog variable with the same name as your activity field variable.
You might play safe: