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

时间:2016-03-02 04:53:58

标签: android

在我解决了我的第一个问题之后,这个问题出现了:(请帮我修理它..

  

03-02 12:47:02.785 9439-9439 / com.ucu.ccs.classrecord   E / WindowManager:android.view.WindowLeaked:Activity   com.ucu.ccs.classrecord.Login已泄露窗口   com.android.internal.policy.impl.PhoneWindow $ {DecorView 6d17cc4   最初添加的V.E ..... R ...... D 0,0-1002,348}               在android.view.ViewRootImpl。(ViewRootImpl.java:465)               在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:277)               在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)               在android.app.Dialog.show(Dialog.java:312)               在com.ucu.ccs.classrecord.Login $ AttemptLogin.onPreExecute(Login.java:158)               在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)               在android.os.AsyncTask.execute(AsyncTask.java:535)               在com.ucu.ccs.classrecord.Login.isOnline(Login.java:113)               在com.ucu.ccs.classrecord.Login $ 1.onClick(Login.java:73)               在android.view.View.performClick(View.java:5197)               在android.view.View $ PerformClick.run(View.java:20926)               在android.os.Handler.handleCallback(Handler.java:739)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:145)               在android.app.ActivityThread.main(ActivityThread.java:5942)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)               在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();
            }
        }
    }

3 个答案:

答案 0 :(得分:25)

如果您的活动已被销毁但对话框仍在显示,则会发生此错误。所以您已在活动的onDestroy()

中添加了这些代码
@Override
public void onDestroy() {
    super.onDestroy();
    if (dialog != null) {
        dialog.dismiss();
        dialog = null;
    }
}

希望这适合你。

答案 1 :(得分:1)

您在退出活动后尝试显示对话框。 当您转移活动时,在 doInBackground 中,在 startActivity(i)之前关闭对话框 试试这个,让我知道它是否有效。

答案 2 :(得分:0)

实际上你的活动已经完成了一些方法,所以你需要关闭对话框。

在onPause()中添加以下代码

if(isFinishing()){
    if (pDialog!= null) {
        pDialog.dismiss();
        pDialog= null;
    }
}