为什么我的应用程序有时在早期启动时出错?

时间:2013-06-25 16:18:22

标签: android android-asynctask

我有一些问题。有时当我试图运行我的应用程序时,它将被强制关闭。我不知道为什么会发生这种情况。这是我的源代码。

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setCancelable(false);
        //AlertDialog.Builder alert = new AlertDialog.Builder(this); 
       .....
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
                no_pasien = no_pas.getText().toString();
                new LoginProses().execute();
                .....
        alert.show();

class LoginProses extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
        ......
    }

    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("no_pasien", no));

        JSONObject json = jsonParser.makeHttpRequest(url_login,
                "POST", params);
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                pasien = json.getJSONArray("pasien");
                JSONObject c = pasien.getJSONObject(0);
                int id = c.getInt("id");

                new Temporary().setIdPasien(id);

                Intent goMainAct = new Intent();
 //                 goMainAct.putExtra("id", id);
                goMainAct.setClass(Login.this, MainActivity.class);
                finish();
                startActivity(goMainAct);
            } else {
                // failed to create product
                Intent getReload = getIntent();
                getReload.putExtra("status", 1);
                finish();
                startActivity(getReload);
            }

        } catch (JSONException e) {
            if(pDialog.isShowing()){ pDialog.dismiss(); }
        }

        return null;
    }
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }
}

这是我的问题的日志错误:

//HERE IS THE LOG:
06-25 22:57:23.836: E/WindowManager(7630): Activity com.iteadstudio.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41939850 that was originally added here
06-25 22:57:23.836: E/WindowManager(7630): android.view.WindowLeaked: Activity com.iteadstudio.Login has leaked window     com.android.internal.policy.impl.PhoneWindow$DecorView@41939850 that was originally added here
06-25 22:57:23.836: E/WindowManager(7630):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
06-25 22:57:23.836: E/WindowManager(7630):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
06-25 22:57:23.836: E/WindowManager(7630):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-25 22:57:23.836: E/WindowManager(7630):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-25 22:57:23.836: E/WindowManager(7630):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
06-25 22:57:23.836: E/WindowManager(7630):  at android.app.Dialog.show(Dialog.java:278)
06-25 22:57:23.836: E/WindowManager(7630):  at com.iteadstudio.Login$LoginProses.onPreExecute(Login.java:122)
06-25 22:57:23.836: E/WindowManager(7630):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-25 22:57:23.836: E/WindowManager(7630):  at android.os.AsyncTask.execute(AsyncTask.java:511)
06-25 22:57:23.836: E/WindowManager(7630):  at com.iteadstudio.Login$3.onClick(Login.java:95)
06-25 22:57:23.836: E/WindowManager(7630):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-25 22:57:23.836: E/WindowManager(7630):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 22:57:23.836: E/WindowManager(7630):  at android.os.Looper.loop(Looper.java:137)
06-25 22:57:23.836: E/WindowManager(7630):  at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 22:57:23.836: E/WindowManager(7630):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 22:57:23.836: E/WindowManager(7630):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 22:57:23.836: E/WindowManager(7630):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-25 22:57:23.836: E/WindowManager(7630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-25 22:57:23.836: E/WindowManager(7630):  at dalvik.system.NativeStart.main(Native Method)
06-25 22:57:23.946: D/dalvikvm(7630): GC_CONCURRENT freed 782K, 6% free 14319K/15203K, paused 4ms+3ms
06-25 22:57:23.976: D/AndroidRuntime(7630): Shutting down VM
06-25 22:57:23.976: W/dalvikvm(7630): threadid=1: thread exiting with uncaught exception     (group=0x40ab4210)
06-25 22:57:23.986: E/AndroidRuntime(7630): FATAL EXCEPTION: main
06-25 22:57:23.986: E/AndroidRuntime(7630): java.lang.IllegalArgumentException: View not attached to window manager
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.app.Dialog.dismissDialog(Dialog.java:321)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.app.Dialog$1.run(Dialog.java:119)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.app.Dialog.dismiss(Dialog.java:306)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at com.iteadstudio.Login$LoginProses.onPostExecute(Login.java:177)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at com.iteadstudio.Login$LoginProses.onPostExecute(Login.java:1)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.os.AsyncTask.finish(AsyncTask.java:602)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.os.Looper.loop(Looper.java:137)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at java.lang.reflect.Method.invoke(Method.java:511)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-25 22:57:23.986: E/AndroidRuntime(7630):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

你可能得到JSONException

catch (JSONException e) {
        if(pDialog.isShowing()){ pDialog.dismiss(); }

关闭对话框,以便您无法在onPostExecute中再次关闭它。

答案 1 :(得分:0)

在活动处于前台之前,您似乎试图显示一个对话框。活动在前台后,确保显示对话框。您可以尝试在mainActivity的onResume()之后启动asyncTask。

答案 2 :(得分:0)

问题是您正在尝试附加到尚未准备好的窗口,将警报对话框代码移动到onResume函数,您应该没问题

@Override
protected void onResume(){
       super.onResume();
       AlertDialog.Builder alert = new AlertDialog.Builder(this);
       alert.setCancelable(false);
        .....
       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
            no_pasien = no_pas.getText().toString();
            new LoginProses().execute();
            .....
       alert.show();
}