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

时间:2017-02-13 18:38:23

标签: android json android-volley

如何设置进度对话框直到服务器响应?我正在尝试,但它在show进度对话框中显示错误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)

错误就像:

{{1}}

1 个答案:

答案 0 :(得分:1)

我认为你应该修改你传递给Context构造函数的ProgressDialog

progressDialog= new ProgressDialog(Loginn.this);

其余的保持不变。