进度条中的运行时异常

时间:2013-10-07 06:11:11

标签: android

if (Commons.HaveNetworkConnection()) {
                if ((txt_USERNAME.getText().toString().trim() != null && txt_USERNAME
                        .getText().toString().length() != 0)// username
                                                            // check
                        && (txt_PASSWORD.getText().toString().trim() != null && txt_PASSWORD
                                .getText().toString().length() != 0)) {// password
                    // check
                    pd = ProgressDialog.show(LoginActivity.this.getApplicationContext(), "","Please wait...");
                    Toast.makeText(getContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();
                    Thread thread = new Thread(LoginActivity.this);
                    thread.start();

                } else {
                    Toast.makeText(getBaseContext(),
                            "Invalid username or password",
                            Toast.LENGTH_LONG).show();
                }
            } else {
                Commons.setOnlineAlert();
            }

}catch(Exception e){Toast.makeText(getBaseContext(),"problem in onclick 1 block"+e,Toast.LENGTH_LONG).show();}
        }

        private Context getContext() {
            // TODO Auto-generated method stub
            return null;
        }

    });

当我点击登录按钮时,发生运行时异常

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

请帮助我解决这个问题

2 个答案:

答案 0 :(得分:1)

你正在显示带有null的toast消息..  删除此方法

 private Context getContext() {
            // TODO Auto-generated method stub
            return null;
        }

并在此行中

Toast.makeText(getContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();

更改为

Toast.makeText(getApplicationContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

Context方法中,您应该实际返回活动的上下文

    private Context getContext() {
        // TODO Auto-generated method stub
        return LoginActivity.this.getApplicationContext();
    }

并在Toast消息中设置如下:

      Toast.makeText(getContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();