Activity在onClick方法中泄露了窗口

时间:2014-05-19 10:03:53

标签: android progressdialog

我已经阅读了很多相同的问题,但我无法理解我做错了什么。 我只想在查询进行时显示对话框(显示加载过程)。 当我按下按钮" Ok"它必须显示执行SQL查询和对话窗口,但它不会。 这是我的代码:

 public OnClickListener btnOkListener = new OnClickListener(){

    @Override
    public void onClick(View v) {

        eWord = etWord.getText().toString();

        switch (v.getId()){

            case R.id.btnOk:
                syns.clear();
                lvMain.setAdapter(adapter);
                dialog=new ProgressDialog(MainActivity.this);
                dialog.setTitle("Please wait");
                dialog.setMessage("Loading");
                dialog.setCancelable(false);
                dialog.setInverseBackgroundForced(false);
                Log.d(LOG_TAG,"Dialog is showing");
                dialog.show();


                String query = // SQL query


                Cursor cursor = database.rawQuery(query, null);

                int wordColIndex = cursor.getColumnIndex(SynDB.Word);
                while (cursor.moveToNext()) {

                    string_word = cursor.getString(wordColIndex);
                    syns.add(string_word);


                }
                displayListView();

                lvMain.setAdapter(adapter);
                cursor.close();
                Log.d(LOG_TAG, "Dialog dies");
                dialog.hide();

                break; 

和我的LogCat日志:

05-19 09:51:50.238    1865-1865/com.psdevteam.SynoDict.app D/MyLogs:﹕ Dialog is showing
05-19 09:51:50.248    1865-1865/com.psdevteam.SynoDict.app D/MyLogs:﹕ Dialog is died
05-19 09:51:51.738    1865-1865/com.psdevteam.SynoDict.app W/KeyCharacterMap﹕ No keyboard for id 65539
05-19 09:51:51.738    1865-1865/com.psdevteam.SynoDict.app W/KeyCharacterMap﹕ Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-19 09:51:54.888    1865-1865/com.psdevteam.SynoDict.app E/WindowManager﹕ Activity com.psdevteam.SynoDict.app.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@b5d2ba98 that was originally added here
android.view.WindowLeaked: Activity com.psdevteam.SynoDict.app.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@b5d2ba98 that was originally added here
        at android.view.ViewRoot.<init>(ViewRoot.java:259)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
        at android.view.Window$LocalWindowManager.addView(Window.java:424)
        at android.app.Dialog.show(Dialog.java:241)
        at com.psdevteam.SynoDict.app.MainActivity$1.onClick(MainActivity.java:93)
        at android.view.View.performClick(View.java:2485)
        at android.view.View$PerformClick.run(View.java:9081)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码,但要小心,您无法在doInBackground更新您的视图,因此如果您想更改您的用户界面,则需要使用以下方法之一:

 onPreExecute
 onPostExecute
 publishProgress
doInBackground

中完成工作时,

publishProgress用于更新UI

  private class GettingData extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        dialog=new ProgressDialog(MainActivity.this);
            dialog.setTitle("Please wait");
            dialog.setMessage("Loading");
            dialog.setCancelable(false);
            dialog.setInverseBackgroundForced(false);
            Log.d(LOG_TAG,"Dialog is showing");
            dialog.show();
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {

            String query = // SQL query


            Cursor cursor = database.rawQuery(query, null);

            int wordColIndex = cursor.getColumnIndex(SynDB.Word);
            while (cursor.moveToNext()) {

                string_word = cursor.getString(wordColIndex);
                syns.add(string_word);
            }

        return null;
    }


        @Override
        protected void onPostExecute(Void result) {
                dialog.dismiss();
                displayListView();

                lvMain.setAdapter(adapter);
                cursor.close();
                Log.d(LOG_TAG, "Dialog dies");

            super.onPostExecute(result);
        }
}