1

在 Android 开发中相当新,我不断收到窗口泄漏异常,可能是由于使用了我制作的警报框。在寻找我必须改变的东西时,我尝试了很多东西,但我找不到我的错误。也许有人可以帮我查看我的代码?

public class LogInActivity extends Activity {
    EditText gebruikersnaamET, paswoordET;
    TextView titel;
    TextView gebruikersnaamTV;
    TextView paswoordTV;
    TextView geenAccount;
    Button ok;
    String url = "";
    final Context context = this;
    String username = "";
    SharedPreferences settings;
    AlertBox alert;
    Gebruiker g = null;
    private ProgressDialog pDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // http://stackoverflow.com/questions/4544302/android-prevent-user-from-coming-back-to-login-page-after-logging-in
        settings = context.getSharedPreferences("CoNeePrefs", 0);
        boolean isLogged = settings.getBoolean("isLogged", false);

        setTitle("Log in");
        setContentView(R.layout.activity_log_in);

        gebruikersnaamET = (EditText) findViewById(R.id.et_un);
        gebruikersnaamET.requestFocus();
        gebruikersnaamET.setTypeface(MyFonts.Fonts.PRESSTART);

        paswoordET = (EditText) findViewById(R.id.et_pw);
        paswoordET.setTypeface(MyFonts.Fonts.PRESSTART);

        ok = (Button) findViewById(R.id.btn_login);
        ok.setTypeface(MyFonts.Fonts.PRESSTART);

        titel = (TextView) findViewById(R.id.titel);
        // titel.setTypeface(MyFonts.Fonts.PRESSTART);
        Typeface face;
        face = Typeface.createFromAsset(getAssets(), "fonts/pressstart.ttf");
        titel.setTypeface(face);

        gebruikersnaamTV = (TextView) findViewById(R.id.tv_un);
        gebruikersnaamTV.setTypeface(MyFonts.Fonts.PRESSTART);

        paswoordTV = (TextView) findViewById(R.id.tv_pw);
        paswoordTV.setTypeface(MyFonts.Fonts.PRESSTART);

        geenAccount = (TextView) findViewById(R.id.nogGeenAccount);
        geenAccount.setTypeface(MyFonts.Fonts.PRESSTART);

        g = new Gebruiker();

        if (isLogged) {
            Intent intent = new Intent(context, MenuActivity.class);
            intent.putExtra("gebruikersnaam",
                    settings.getString("gebruikersnaam", "none"));
            startActivity(intent);
            finish();
        } else {
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    new checkUserDetails().execute();
                }
            });

        }

        geenAccount.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    // http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
    class checkUserDetails extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LogInActivity.this);
            pDialog.setMessage("In aan het loggen...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... params) {

            url = "http://iwtsl.ehb.be/~iris.vdz/check.php";
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("gebruikersnaam",
                    gebruikersnaamET.getText().toString()));
            postParameters.add(new BasicNameValuePair("paswoord", paswoordET
                    .getText().toString()));
            // String valid = "1";
            String res = null;
            String username = gebruikersnaamET.getText().toString();

            try {
                res = DatabaseAccess.getInstance().phpUitvoeren(postParameters,
                        url);

                if (res.equals("1")) {
                    g = DatabaseAccess.getInstance().getUser(username);
                    String adres = g.getStraat() + " - " + g.getPostcode()
                            + " " + g.getGemeente();
                    String volledigeNaam = g.getVoornaam() + " " + g.getNaam();
                    settings = context.getSharedPreferences("CoNeePrefs", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("isLogged", true);
                    editor.putString("gebruikersnaam", g.getGebruikersnaam());
                    editor.putString("naam", volledigeNaam);
                    editor.putString("e-mail", g.getEmail());
                    editor.putString("adres", adres);
                    editor.putString("paswoord",
                            g.getEncodedPaswoord(g.getPaswoord()));
                    editor.commit();

                    Intent intent = new Intent(context, MenuActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    alert = new AlertBox(context, "Error",
                            "Er is iets misgegaan, probeer opnieuw!\nMogelijke redenen:\n"
                                    + "- u heeft geen internetverbinding\n"
                                    + "- u heeft geen juiste login ingegeven\n"
                                    + "- er is een probleem met het netwerk\n"
                                    + "- er is een probleem met de database");
                }

            } catch (Exception e) {
                alert = new AlertBox(context, "Error", e.toString());

            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }

这是警报框的代码:

public class AlertBox {

    public AlertBox(Context context, String title, String mymessage){
         new AlertDialog.Builder(context)
          .setMessage(mymessage)
          .setTitle(title)
          .setCancelable(true)
          .setNeutralButton(android.R.string.cancel,
             new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton){

             }})
          .show();
    }
}

这是我得到的堆栈跟踪:

04-07 14:31:28.230:E/AndroidRuntime(10287):致命异常:AsyncTask #1
04-07 14:31:28.230: E/AndroidRuntime(10287): java.lang.RuntimeException: 执行 doInBackground() 时出错
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.os.AsyncTask$3.done(AsyncTask.java:200)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.lang.Thread.run(Thread.java:1019)
04-07 14:31:28.230:E/AndroidRuntime(10287):原因:java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.os.Handler.(Handler.java:121)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.app.Dialog.(Dialog.java:101)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.app.AlertDialog.(AlertDialog.java:63)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.app.AlertDialog$Builder.create(AlertDialog.java:797)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.app.AlertDialog$Builder.show(AlertDialog.java:812)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 be.iwt.ehb.conee.extrafeatures.AlertBox.(AlertBox.java:20)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 be.iwt.ehb.conee.activities.LogInActivity$checkUserDetails.doInBackground(LogInActivity.java:178)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 be.iwt.ehb.conee.activities.LogInActivity$checkUserDetails.doInBackground(LogInActivity.java:1)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 android.os.AsyncTask$2.call(AsyncTask.java:185)
04-07 14:31:28.230: E/AndroidRuntime(10287): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-07 14:31:28.230: E/AndroidRuntime(10287): ... 4 更多
04-07 14:31:28.661: W/IInputConnectionWrapper(10287): showStatusIcon on inactive InputConnection
04-07 14:31:28.681: D/dalvikvm(10287): GC_CONCURRENT 释放 200K,49% 释放 2915K/5639K,外部 303K/558K,暂停 18ms+62ms
04-07 14:31:29.381: E/WindowManager(10287): Activity be.iwt.ehb.conee.activities.LogInActivity 泄露了最初添加的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@40538cb0这里
04-07 14:31:29.381: E/WindowManager(10287): android.view.WindowLeaked: Activity be.iwt.ehb.conee.activities.LogInActivity 已泄露窗口 com.android.internal.policy.impl.PhoneWindow$DecorView @40538cb0 最初添加在这里
04-07 14:31:29.381: E/WindowManager(10287): 在 android.view.ViewRoot.(ViewRoot.java:259)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.view.Window$LocalWindowManager.addView(Window.java:465)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.app.Dialog.show(Dialog.java:241)
04-07 14:31:29.381: E/WindowManager(10287): 在 be.iwt.ehb.conee.activities.LogInActivity$checkUserDetails.onPreExecute(LogInActivity.java:127)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.os.AsyncTask.execute(AsyncTask.java:391)
04-07 14:31:29.381: E/WindowManager(10287): 在 be.iwt.ehb.conee.activities.LogInActivity$1.onClick(LogInActivity.java:96)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.view.View.performClick(View.java:2506)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.view.View$PerformClick.run(View.java:9112)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.os.Handler.handleCallback(Handler.java:587)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.os.Handler.dispatchMessage(Handler.java:92)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.os.Looper.loop(Looper.java:130)
04-07 14:31:29.381: E/WindowManager(10287): 在 android.app.ActivityThread.main(ActivityThread.java:3835)
04-07 14:31:29.381: E/WindowManager(10287): 在 java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:31:29.381: E/WindowManager(10287): 在 java.lang.reflect.Method.invoke(Method.java:507)
04-07 14:31:29.381: E/WindowManager(10287): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-07 14:31:29.381: E/WindowManager(10287): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-07 14:31:29.381: E/WindowManager(10287): at dalvik.system.NativeStart.main(Native Method)

提前非常感谢!

4

1 回答 1

0

窗口泄漏不是您崩溃的根本原因;这里的主要问题是您试图从后台线程创建一个对话框:

原因:java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序

在 AlertBox 中,您正在创建一个新对话框,该对话框又包含它们自己的处理程序:

new AlertDialog.Builder(context)
          [...]
          .show();

但是,您正在从 AsyncTask 中创建 AlertBox doInBackground()

new AlertBox(context, "Error", e.toString());

将该逻辑移到主线程中,例如通过处理onPostExecute().

其次,您会收到“窗口泄漏”消息,因为 AsyncTask 在活动消失时仍在运行,并且它包含对附加到该窗口的对象的引用,特别是pDialog.

于 2013-04-07T12:56:32.157 回答