当前位置: 代码迷 >> Android >> 如何设置进度对话框,直到服务器响应? 我正在尝试,但是在进度对话框的显示方法上显示错误#Json #Volley #StringRequest
  详细解决方案

如何设置进度对话框,直到服务器响应? 我正在尝试,但是在进度对话框的显示方法上显示错误#Json #Volley #StringRequest

热度:53   发布时间:2023-08-04 10:38:30.0

如何设置进度对话框,直到服务器响应? 我正在尝试,但是在进度对话框的显示方法中显示错误Json Volley StringRequest

public class Loginn extends Activity { 
Button btn1,btn2,btn3;
EditText et1,et2;
TextView waittv;
SharedPreferences sp;
CheckBox cb1;
ProgressDialog pd;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    btn1=(Button)findViewById(R.id.btn1);
    btn2=(Button)findViewById(R.id.btn2);
    btn3=(Button)findViewById(R.id.regbtn);
    waittv=(TextView)findViewById(R.id.waittv);
    et1=(EditText)findViewById(R.id.et1);
    et2=(EditText)findViewById(R.id.et2);
    cb1=(CheckBox)findViewById(R.id.cb1);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String mobile = et1.getText().toString();
            String pword = et2.getText().toString();
            if (mobile.isEmpty()) {
                et1.setError("please enter Mobile no.");
            } else if (pword.isEmpty()) {
                et2.setError("please enter password");
            } else { 
                //progressDialog= new ProgressDialog(getApplicationContext());
                //progressDialog.setTitle("Wait for login ...");
                //progressDialog.setMessage("if it takes much time,then check your internet connectivity...");
                //progressDialog.show();
                Response.Listener<String> listener=new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {

                            JSONObject jsonResponse =new JSONObject(response);

                            boolean success=jsonResponse.getBoolean("success");

                            if(success){
                                String fname=jsonResponse.getString("first_name");
                                String lname=jsonResponse.getString("last_name");                                
             Toast.makeText(getApplicationContext(),"Login Success", Toast.LENGTH_LONG).show();


                                //progressDialog.dismiss();

                                Intent intent=new Intent(getApplicationContext(),Profile.class);           

                                Loginn.this.startActivity(intent);
                            }
                            else
                            {                                   Toast.makeText(getApplicationContext(),"Incorrect mobile/password", Toast.LENGTH_LONG).show();
                                pd.dismiss();
                                //progressDialog.dismiss();                                   
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                LoginRequest loginRequest=new LoginRequest(mobile,pword,listener);
                RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
                queue.add(loginRequest);                 
            }
        }
    });
}

错误如:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                      at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                      at android.app.Dialog.show(Dialog.java:319)
                      at android.app.ProgressDialog.show(ProgressDialog.java:116)
                      at android.app.ProgressDialog.show(ProgressDialog.java:99)
                      at android.app.ProgressDialog.show(ProgressDialog.java:94)

我认为您应该修复更改??传递给ProgressDialog构造函数的Context

progressDialog= new ProgressDialog(Loginn.this);

其余的保持不变。

  相关解决方案