Android:Window Manager:android视图窗口泄露

前端之家收集整理的这篇文章主要介绍了Android:Window Manager:android视图窗口泄露前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我解决了我的第一个问题之后,这一个出现了:(请帮我解决它..

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
V.E….. 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.Handler.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)

这是我的代码

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();
            }
        }
    }

解决方法

如果您的活动已被销毁但对话框仍在显示,则会发生此错误.所以你已经在你的活动的onDestroy()中添加了这些代码
@Override
public void onDestroy() {
    super.onDestroy();
    if (dialog != null) {
        dialog.dismiss();
        dialog = null;
    }
}

希望这对你有用.

原文链接:https://www.f2er.com/android/313884.html

猜你在找的Android相关文章