1

I have some problem. sometimes when I just try to run my application, it is going to be force close. I don't know why it is going to be happened. here are my source code.

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 error for my problem:

//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)
Hendra
  • 21
  • 1
  • 9

3 Answers3

0

You probably get JSONException

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

dismiss a dialog so You can't dismiss it again in onPostExecute.

Gustek
  • 3,680
  • 2
  • 22
  • 36
  • so in onPostExecute i must add the same code? i mean like if(pDialog.isShowing()){ pDialog.dismiss(); } – Hendra Jun 25 '13 at 22:32
  • PostExecute will be always called so do it just there, catch block should be used to handle exception, for example inform the user there was a problem. – Gustek Jun 26 '13 at 00:07
0

Looks like you are trying to show a dialog box before your activity is in the foreground. Make sure that you are showing the dialog box after your activity is in the foreground. You may try starting your asyncTask after onResume() of your mainActivity.

deepdroid
  • 633
  • 5
  • 26
0

The problem is that you're trying to attach to a window that is not ready yet, move your alert dialog code to your onResume function and you should be fine

@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();
}
Mr.Me
  • 9,192
  • 5
  • 39
  • 51