0

I am getting force close when a button is pressed more than once. I have a condition like if internet is connected then google maps will be shown .If internet is not connected then i have kept a ALERT BOX displaying to check our internet connection .So the problem lies here. If internet is not connected when the button is pressed ONCE the alertbox is displayed. But if clicked more than once it shows a FORCE CLOSE. I have placed the alertbox display in catch block..

   case R.id.geocode_button:   // Place name as selected by the user.

            placeName = placeText.getText().toString();
            clik=1;         
            placenametemp = placeName;
            System.out.println(""+placenametemp);

            String [] optionArray = new String[numberOptions];
            Geocoder gcoder = new Geocoder(this);

            try
            { 
                              ///// code to display google map 


            } 
            catch (IOException e)
            {
            if(!dialogShown)
                {
                    dialogShown = true;
                    //Show your dialog

                new  
        AlertDialog.Builder(this).setIcon(R.drawable.icon).setTitle(
                " Check your Internet  
        Connection!").setPositiveButton("OK",
                new DialogInterface.OnClickListener()
                {

                    @Override
                    public void onClick(DialogInterface dialog,  
                     int which)
                    {
                        dialogShown = false;    
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                }).show();

                Log.e("Geocoder", "I/O Failure; is network    
                                                                available?",e);
            }
                          }


//The LOG cat details :


    12-10 18:31:09.506: E/Geocoder(238): I/O Failure; is network available?
    12-10 18:31:09.506: E/Geocoder(238): java.io.IOException: Unable to parse response from server
    12-10 18:31:09.506: E/Geocoder(238):    at android.location.Geocoder.getFromLocationName(Geocoder.java:166)
    12-10 18:31:09.506: E/Geocoder(238):    at com.journey.snoozer.Mapper.onClick(Mapper.java:93)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.View.performClick(View.java:2364)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.View.onTouchEvent(View.java:4179)
    12-10 18:31:09.506: E/Geocoder(238):    at android.widget.TextView.onTouchEvent(TextView.java:6540)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.View.dispatchTouchEvent(View.java:3709)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:09.506: E/Geocoder(238):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    12-10 18:31:09.506: E/Geocoder(238):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    12-10 18:31:09.506: E/Geocoder(238):    at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    12-10 18:31:09.506: E/Geocoder(238):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    12-10 18:31:09.506: E/Geocoder(238):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    12-10 18:31:09.506: E/Geocoder(238):    at android.os.Handler.dispatchMessage(Handler.java:99)
    12-10 18:31:09.506: E/Geocoder(238):    at android.os.Looper.loop(Looper.java:123)
    12-10 18:31:09.506: E/Geocoder(238):    at android.app.ActivityThread.main(ActivityThread.java:4363)
    12-10 18:31:09.506: E/Geocoder(238):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-10 18:31:09.506: E/Geocoder(238):    at java.lang.reflect.Method.invoke(Method.java:521)
    12-10 18:31:09.506: E/Geocoder(238):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    12-10 18:31:09.506: E/Geocoder(238):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    12-10 18:31:09.506: E/Geocoder(238):    at dalvik.system.NativeStart.main(Native Method)
    12-10 18:31:10.277: E/ActivityThread(238): Failed to find provider info for com.google.settings
    12-10 18:31:10.296: E/ActivityThread(238): Failed to find provider info for com.google.settings
    12-10 18:31:10.426: E/ActivityThread(238): Failed to find provider info for com.google.settings
    12-10 18:31:11.626: I/MapActivity(238): Handling network change notification:CONNECTED
    12-10 18:31:11.626: E/MapActivity(238): Couldn't get connection factory client
    12-10 18:31:11.926: D/dalvikvm(238): GC freed 3247 objects / 233880 bytes in 241ms
    12-10 18:31:11.936: E/DEBUGTAG(238): Remote Image Exception
    12-10 18:31:11.936: E/DEBUGTAG(238): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.view.View.requestLayout(View.java:7918)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.view.View.requestLayout(View.java:7918)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.view.View.requestLayout(View.java:7918)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.view.ViewGroup.removeAllViews(ViewGroup.java:2146)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:196)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.app.Activity.setContentView(Activity.java:1622)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at com.journey.snoozer.ShowTheMap.mapProgress(ShowTheMap.java:143)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at com.journey.snoozer.ShowTheMap$loadImageTask.doInBackground(ShowTheMap.java:197)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at com.journey.snoozer.ShowTheMap$loadImageTask.doInBackground(ShowTheMap.java:1)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    12-10 18:31:11.936: E/DEBUGTAG(238):    at java.lang.Thread.run(Thread.java:1096)
    12-10 18:31:12.756: D/dalvikvm(238): GC freed 7692 objects / 597720 bytes in 68ms
    12-10 18:31:12.886: D/dalvikvm(238): GC freed 5342 objects / 404384 bytes in 62ms
    12-10 18:31:13.296: E/WindowManager(238): Activity com.journey.snoozer.Mapper has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44ea2bc8 that was originally added here
    12-10 18:31:13.296: E/WindowManager(238): android.view.WindowLeaked: Activity com.journey.snoozer.Mapper has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44ea2bc8 that was originally added here
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewRoot.<init>(ViewRoot.java:227)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
    12-10 18:31:13.296: E/WindowManager(238):   at android.app.Dialog.show(Dialog.java:239)
    12-10 18:31:13.296: E/WindowManager(238):   at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
    12-10 18:31:13.296: E/WindowManager(238):   at com.journey.snoozer.Mapper.onClick(Mapper.java:141)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.View.performClick(View.java:2364)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.View.onTouchEvent(View.java:4179)
    12-10 18:31:13.296: E/WindowManager(238):   at android.widget.TextView.onTouchEvent(TextView.java:6540)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.View.dispatchTouchEvent(View.java:3709)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    12-10 18:31:13.296: E/WindowManager(238):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    12-10 18:31:13.296: E/WindowManager(238):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    12-10 18:31:13.296: E/WindowManager(238):   at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    12-10 18:31:13.296: E/WindowManager(238):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    12-10 18:31:13.296: E/WindowManager(238):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    12-10 18:31:13.296: E/WindowManager(238):   at android.os.Handler.dispatchMessage(Handler.java:99)
    12-10 18:31:13.296: E/WindowManager(238):   at android.os.Looper.loop(Looper.java:123)
    12-10 18:31:13.296: E/WindowManager(238):   at android.app.ActivityThread.main(ActivityThread.java:4363)
    12-10 18:31:13.296: E/WindowManager(238):   at java.lang.reflect.Method.invokeNative(Native Method)
    12-10 18:31:13.296: E/WindowManager(238):   at java.lang.reflect.Method.invoke(Method.java:521)
    12-10 18:31:13.296: E/WindowManager(238):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    12-10 18:31:13.296: E/WindowManager(238):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    12-10 18:31:13.296: E/WindowManager(238):   at dalvik.system.NativeStart.main(Native Method)
nayab
  • 2,332
  • 1
  • 20
  • 34
Sindu
  • 215
  • 3
  • 6
  • 16

2 Answers2

0

You can not show the alert dialog more than one time so put a check like this

first create a class level variable

public boolean dialogShown = false;

then in here case R.id.geocode_button

if(!dialogShown){
dialogShown = true;
//Show your dialog
}

then in your dialog ok button click listener

dialogShown = false;

Update

check your internet connection like this

 void boolean checkInternet(){
  NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    return false;
  if (!i.isConnected())
    return false;
  if (!i.isAvailable())
    return false;
  return true;
}
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
  • I have updated my catch block..Sir i guess the problem is not because of alert box coz just tried the same with Toast msg and i am still getting force close when the button is pressed many times – Sindu Dec 10 '12 at 11:51
  • Test it by putting this line in try block `Geocoder gcoder = new Geocoder(this);` – Ali Imran Dec 10 '12 at 11:55
  • also catch this exception also `catch (Exception e)` – Ali Imran Dec 10 '12 at 11:56
  • sir the problem is not in the catch block ..i have removed everything inside catch block ..Wen the button is clicked many times it shows force close – Sindu Dec 10 '12 at 12:53
  • You need to first check the internet connection before doing any processing but before that check the dialog shown status at the first line of your click event. then perform your rest of processing. – Ali Imran Dec 10 '12 at 13:28
0

Right. So first of all, that's not the way to check if internet connection is available. But that is not the problem here. The first problem is the CalledFromTheWrongThread exception, wich means that somewhere you are trying to access UI elements from another thread. Don't ever do that. I'm guessing you have an AsyncTask trying to post some update in a way that is not allowed. (at com.journey.snoozer.ShowTheMap.mapProgress(ShowTheMap.java:143))

The main issue here though is the WindowLeaked exception a few lines down. That happens when you, for example, exit an activity and then try to access a Dialog. (Orientation changes etc.) Since you had the other exception with an AsyncTask involved I wonder if you cancel the first AsycTask when the IO exception occurs.

So make sure you use .cancel() on the AsyncTask in onPause() and dissmiss() on the dialog that the task creates.

Qw4z1
  • 3,041
  • 1
  • 24
  • 36