Android : Window Manager : android view window leaked
Right after I fix my first problem this one comes up :( please help me fix it..
03-02 12:47:02.785 9439-9439/com.ucu.ccs.classrecord E/WindowManager﹕ android.view.WindowLeaked: Activity com.ucu.ccs.classrecord.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{6d17cc4 VE.... R......D 0,0-1002,348} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:465) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:277) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:312) at com.ucu.ccs.classrecord.Login$AttemptLogin.onPreExecute(Login.java:158) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.ucu.ccs.classrecord.Login.isOnline(Login.java:113) at com.ucu.ccs.classrecord.Login$1.onClick(Login.java:73) at android.view.View.performClick(View.java:5197) at android.view.View$PerformClick.run(View.java:20926) at android.os.Handler.handleCallback(Handler.java:739) at android.os.H andler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
here is my code:
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String instructor_id = editUser.getText().toString().trim();
String password = editPass.getText().toString().trim();
SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
preferences.edit().putString("password", password).commit();
preferences.edit().putString("inst_id", instructor_id).commit();
if (editUser.getText().toString().equals("") || editPass.getText().toString().equals("")){
Toast.makeText(getApplicationContext(),"Please enter Instructor ID and Password", Toast.LENGTH_LONG).show();
}else {
isOnline();
}
}
});
public boolean isOnline(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()){
new AttemptLogin().execute();
}else {
checkInternet();
}
return false;
}
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure = false;
String inst_id = editUser.getText().toString();
String password = editPass.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
int success;
try{
List<NameValuePair> mList = new ArrayList<NameValuePair>();
mList.add(new BasicNameValuePair("instructor_id", inst_id));
mList.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", mList);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, MainActivity.class);
startActivity(i);
//finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
}catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
session.setLogin(true);
pDialog.dismiss();
if (s != null){
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
}
This error will happen if your activity has been destroyed but you dialog is still showing. So You have added these code in your activity's onDestroy()
@Override
public void onDestroy() {
super.onDestroy();
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
Hope this works for you.
You're trying to show a Dialog after you've exited an Activity. In your doInBackground when you are shifting your activity dismiss the dialog before startActivity(i) try this and let me know if it works.
Actually your activity is getting finished some how, so you need to close dialog.
In onPause() add below code
if(isFinishing()){
if (pDialog!= null) {
pDialog.dismiss();
pDialog= null;
}
}
链接地址: http://www.djcxy.com/p/19940.html
上一篇: 隐藏自定义对话框时窗口被泄漏