asynctask中的对话框错误

发布于 2024-12-10 14:53:32 字数 1709 浏览 0 评论 0原文

当我尝试显示异步任务的工作对话框时,出现以下错误: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-12-17 14:53:32

您可以通过在 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);
    }

You are hiding your member dialog in your code by defining a local variable dialog in your onLogin(...) method. Because of this, dialog is never initialized to anything and that is why you get an NPE in your Login class.

    @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);
    }
ˉ厌 2024-12-17 14:53:32

当您关闭对话框时,您确定对话框存在吗?我认为在 onLogin 中,您重新创建了一个与活动字段变量同名的新对话框变量。

protected void onPostExecute(CloudApp api) {

            dialog.dismiss(); // this one is not initialized
            try {
                CloudAppAccount acc = api.getAccountDetails();

你可以谨慎行事:

 if (dialog != null) dialog.dismiss();

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.

protected void onPostExecute(CloudApp api) {

            dialog.dismiss(); // this one is not initialized
            try {
                CloudAppAccount acc = api.getAccountDetails();

You might play safe:

 if (dialog != null) dialog.dismiss();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文