0

多次按下按钮时,我会强制关闭。我有一个条件,比如如果连接了互联网,那么将显示谷歌地图。如果没有连接互联网,那么我会一直显示警报框以检查我们的互联网连接。所以问题就在这里。如果在按下按钮一次时未连接互联网,则会显示警报框。但是,如果多次单击它会显示强制关闭。我已将警报框显示放置在 catch 块中。

   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)
4

2 回答 2

0

您不能多次显示警报对话框,因此请像这样进行检查

首先创建一个类级变量

public boolean dialogShown = false;

然后在这里case R.id.geocode_button

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

然后在您的对话框ok按钮中单击listener

dialogShown = false;

更新

像这样检查您的互联网连接

 void boolean checkInternet(){
  NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    return false;
  if (!i.isConnected())
    return false;
  if (!i.isAvailable())
    return false;
  return true;
}
于 2012-12-10T10:45:45.120 回答
0

正确的。所以首先,这不是检查互联网连接是否可用的方法。但这不是这里的问题。第一个问题是 CalledFromTheWrongThread 异常,这意味着您正试图从另一个线程访问 UI 元素。永远不要那样做。我猜你有一个 AsyncTask 试图以不允许的方式发布一些更新。(在 com.journey.snoozer.ShowTheMap.mapProgress(ShowTheMap.java:143))

不过这里的主要问题是几行之后的 WindowLeaked 异常。例如,当您退出活动然后尝试访问对话框时,就会发生这种情况。(方向更改等)由于您遇到了涉及 AsyncTask 的另一个异常,我想知道您是否在发生 IO 异常时取消了第一个 AsycTask。

因此,请确保在 onPause() 中的 AsyncTask 上使用 .cancel() 并在任务创建的对话框中使用 dissmiss()。

于 2012-12-11T02:37:04.483 回答