gpt4 book ai didi

android - Activity 在 onClick 方法中泄漏了窗口

转载 作者:行者123 更新时间:2023-11-30 02:53:23 25 4
gpt4 key购买 nike

我读过很多相同的问题,但我不明白我做错了什么。我只想在查询进行时显示对话框(以显示加载过程)。当我按下“确定”按钮时,它会执行 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)

最佳答案

你可以使用下面的代码,但要小心,你不能在 doInBackground 中更新你的 View ,所以如果你想改变你的 UI,你需要使用以下方法之一:

 onPreExecute
onPostExecute
publishProgress

publishProgress 用于在 doInBackground 中执行工作时更新 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);
}
}

关于android - Activity 在 onClick 方法中泄漏了窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com