0

I have an android application that using JSON or mysql for the database. in this case, user must input certain number, and if it is wrong, it gonna called own activity and say that the inputted number is wrong. I try after five times or more or sometimes, I do wrong inputted number and it's gonna be force close. I don't understand why it is gonna be happened. here is my sample code

Intent start = getIntent();
    try{
        int cekStart = start.getExtras().getInt("status");
        Toast.makeText(getBaseContext(), "Maaf Tidak Terdaftar", 3).show();
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setCancelable(false);
        //AlertDialog.Builder alert = new AlertDialog.Builder(this); 
        alert.setTitle("Login"); 
        alert.setMessage("Masukkan Data Anda"); 

        // Set an EditText view to get user input 
        LinearLayout lay=new LinearLayout(this); 
        lay.setOrientation(LinearLayout.VERTICAL); 
        final EditText no_pas = new EditText(this); 
        no_pas.setHint("Nomor Pasien"); 
        lay.addView(no_pas); 
        alert.setView(lay); 
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) {
                if(no_pasien==null)
                no_pasien = no_pas.getText().toString();
                new LoginProses().execute();

            } 
        }); 
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
                finish();
            } 
        }); 
        alert.show();
    }catch (Exception e) {
        // TODO: handle exception
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setCancelable(false);
        //AlertDialog.Builder alert = new AlertDialog.Builder(this); 
        alert.setTitle("Login"); 
        alert.setMessage("Masukkan Data Anda"); 

        // Set an EditText view to get user input 
        LinearLayout lay=new LinearLayout(this); 
        lay.setOrientation(LinearLayout.VERTICAL); 
        final EditText no_pas = new EditText(this); 
        no_pas.setHint("Nomor Pasien"); 
        lay.addView(no_pas); 
        alert.setView(lay); 
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
                no_pasien = no_pas.getText().toString();
                new LoginProses().execute();

            } 
        }); 
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
                finish();
            } 
        }); 
        alert.show();
    }


}

class LoginProses extends AsyncTask<String, String, String> {

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

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String no = no_pasien;

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("no_pasien", no));

        // getting JSON Object
        // Note that create product url accepts POST method

        JSONObject json = jsonParser.makeHttpRequest(url_login,
                "POST", params);

        // check log cat fro response
         //Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent goMainAct = new Intent();
                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) {
            e.printStackTrace();
        }

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

----UPDATE ERROR LOG----- This is the error Log..

06-24 05:28:36.257: E/WindowManager(12036): Activity com.iteadstudio.Login has leaked     window com.android.internal.policy.impl.PhoneWindow$DecorView@4179ce28 that was originally added here
06-24 05:28:36.257: E/WindowManager(12036): android.view.WindowLeaked: Activity com.iteadstudio.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4179ce28 that was originally added here
06-24 05:28:36.257: E/WindowManager(12036):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
06-24 05:28:36.257: E/WindowManager(12036):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
06-24 05:28:36.257: E/WindowManager(12036):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-24 05:28:36.257: E/WindowManager(12036):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-24 05:28:36.257: E/WindowManager(12036):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
06-24 05:28:36.257: E/WindowManager(12036):     at android.app.Dialog.show(Dialog.java:278)
06-24 05:28:36.257: E/WindowManager(12036):     at com.iteadstudio.Login$LoginProses.onPreExecute(Login.java:122)
06-24 05:28:36.257: E/WindowManager(12036):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-24 05:28:36.257: E/WindowManager(12036):     at android.os.AsyncTask.execute(AsyncTask.java:511)
06-24 05:28:36.257: E/WindowManager(12036):     at com.iteadstudio.Login$1.onClick(Login.java:67)
06-24 05:28:36.257: E/WindowManager(12036):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-24 05:28:36.257: E/WindowManager(12036):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 05:28:36.257: E/WindowManager(12036):     at android.os.Looper.loop(Looper.java:137)
06-24 05:28:36.257: E/WindowManager(12036):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-24 05:28:36.257: E/WindowManager(12036):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 05:28:36.257: E/WindowManager(12036):     at java.lang.reflect.Method.invoke(Method.java:511)
06-24 05:28:36.257: E/WindowManager(12036):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-24 05:28:36.257: E/WindowManager(12036):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-24 05:28:36.257: E/WindowManager(12036):     at dalvik.system.NativeStart.main(Native  Method)
06-24 05:28:36.257: D/AndroidRuntime(12036): Shutting down VM
06-24 05:28:36.257: W/dalvikvm(12036): threadid=1: thread exiting with uncaught exception (group=0x40aae210)
06-24 05:28:36.267: E/AndroidRuntime(12036): FATAL EXCEPTION: main
06-24 05:28:36.267: E/AndroidRuntime(12036): java.lang.IllegalArgumentException: View not attached to window manager
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.app.Dialog.dismissDialog(Dialog.java:321)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.app.Dialog$1.run(Dialog.java:119)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.app.Dialog.dismiss(Dialog.java:306)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at com.iteadstudio.Login$LoginProses.onPostExecute(Login.java:177)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at com.iteadstudio.Login$LoginProses.onPostExecute(Login.java:1)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.os.AsyncTask.finish(AsyncTask.java:602)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.os.Looper.loop(Looper.java:137)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at 

java.lang.reflect.Method.invoke(Method.java:511)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at  

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-24 05:28:36.267: E/AndroidRuntime(12036):    at dalvik.system.NativeStart.main(Native Method)

Login.java

 class LoginProses extends AsyncTask<String, String, String> { //line 110
    protected void onPreExecute() {..} //line 117
    protected String doInBackground(String... args) {...} //line 128
    protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
    pDialog.dismiss(); // line 177
    }
Hendra
  • 21
  • 1
  • 9
  • We need to see the error in the logcat. – Stephane Mathis Jun 23 '13 at 21:22
  • @StephaneMathis That is the error log. thanks for remind me about it. do you have any idea after see the error log? – Hendra Jun 23 '13 at 22:35
  • you are using async task. because it says that your activity is leaking. you just need to fix that leaking part. – Gangnaminmo Ako Jun 24 '13 at 02:04
  • What's happening on line 177 of Login.java ? I think you need to be sure that the dialog is still visible before dismissing it. – Stephane Mathis Jun 24 '13 at 08:01
  • @GangnaminmoAko how do I know which one the leaking part? For I tried in one until four times, it is gonna be ok, but when I tried the fifth one, it is going to be force close. I don't understand at all. – Hendra Jun 24 '13 at 18:51
  • @StephaneMathis Login.java line 177 is pDialog.dismiss(); I'll post for update. please help – Hendra Jun 24 '13 at 18:52
  • Just add if(pDialog.isShowing()) pDialog.dismiss(); – Stephane Mathis Jun 24 '13 at 20:24
  • Just to add to @StephaneMathis, you need to add the `if(pDialog.isShowing()) { pDialog.dismiss(); }` inside your `catch` in the `AsyncTask` as when the current `AsyncTask` doesn't complete correctly and throws an exception, it doesn't get to dismiss the `Dialog` currently showing, and so the `Dialog` window is leaked. – Darwind Jun 24 '13 at 20:50
  • @Hendra you mentioned that if its wrong it will call the same Activity. Just use finish(); when you are calling the activity again. – Gangnaminmo Ako Jun 25 '13 at 01:54
  • @StephaneMathis thanks a lot for the help. so it just trick to response the catch exception. but I have still not understand, what is window is leaked mean?? – Hendra Jun 25 '13 at 02:51
  • @Darwind thanks for the explanation, so I get right place to add the code. the same question, what is "window is leaked" mean? would you mind to explain to me? I just didn't get it at all. – Hendra Jun 25 '13 at 02:52
  • @GangnaminmoAko is there any example? because I try just finish(); the application is stoping like I close the application and not showing up again. – Hendra Jun 25 '13 at 02:54
  • @Hendra where did you put finish();? can you post the code where you put it? – Gangnaminmo Ako Jun 25 '13 at 02:56
  • @Hendra the window (your `Dialog`) is the leaked window. See here: http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added for an explanation. – Darwind Jun 25 '13 at 05:38
  • @GangnaminmoAko if{ ....... }else{ Intent getReload = getIntent(); getReload.putExtra("status", 1); finish(); } – Hendra Jun 25 '13 at 06:31
  • @Darwind ok, thanks for your help. I got it right now. :D – Hendra Jun 25 '13 at 06:33

0 Answers0