控制到AsyncTask后应用程序崩溃

时间:2014-08-02 20:52:44

标签: android android-asynctask

我正在使用MapQuest开发导航应用。我从editText获取目标并尝试在AsyncTask中搜索位置并将其显示在地图上。

执行此过程时应用程序崩溃。

我在按钮的onclick方法中执行AsyncTask类(带目标)。 onPreExecute方法工作正常,但在它之后崩溃。

    private class SearchDestination extends AsyncTask<String, Void, GeoPoint>{

    private ProgressDialog dialog= new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
         dialog.setMessage("Searching Destination...");
         dialog.show();
         dialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                SearchDestinationTask.this.cancel(true);

            }
        });
    }

    @Override
    protected GeoPoint doInBackground(String... destination) {
         String str_destination=destination[0];
         List<Address> addresses;
         try{
             Geocoder geocoder= new Geocoder(MainActivity.this, Locale.getDefault());
             addresses=geocoder.getFromLocationName(str_destination, MAX_RESULTS);

         }catch(IOException e1){

             Log.d("MainActivity",
                        "Starting second try...");

             try{
                 Geocoder geocoder= new Geocoder(MainActivity.this,
                 Locale.getDefault());
                 addresses=geocoder.getFromLocationName(str_destination, MAX_RESULTS);

             }catch(IOException e2){
                 Log.e("MainActivity","IO Exception in searching for destination. This
                 is the error message: "    + e2.getMessage());

                 Toast.makeText(getBaseContext(), "Destination could not be found", 

                Toast.LENGTH_SHORT).show();

                 return null;
             }
         }

         if(addresses.isEmpty()){
             Toast.makeText(getBaseContext(), "No Destination found", 

               Toast.LENGTH_SHORT).show();
             return null;
             }else{
                 Log.d("MainActivity", "Located destination sucessfully.");

                 GeoPoint result=new GeoPoint(addresses.get(0).getLatitude(), 

                addresses.get(0).getLongitude());
                 return result;
             }

    }


    @Override
    protected void onPostExecute(GeoPoint result) {

        dialog.dismiss();

        if(result!=null){
            addDestinationOverlay(result);

            if(route.getText()=="Start"){
                route.setText("Route");
                rm.clearRoute();
        }
    }
}
}

private void addDestinationOverlay(GeoPoint destination) {
    // Create a GeoPoint object of the current location and the destination
    GeoPoint currentLocation = new GeoPoint(myLocationOverlay
            .getMyLocation().getLatitude(), myLocationOverlay
            .getMyLocation().getLongitude());

    // Also set the coordinates of the destination for the NaviActivity
    this.destination_coords = new double[] { destination.getLatitude(),
            destination.getLongitude() };

    // Clear previous overlays first
    if (map.getOverlays().size() > 1) {
        map.getOverlays().remove(1);
    }

    // Create the destination overlay
    OverlayItem oi_destination = new OverlayItem(destination,
            "Destination", str_destination);
    final DefaultItemizedOverlay destinationOverlay = new DefaultItemizedOverlay(
            getResources().getDrawable(R.drawable.destination_flag));
    destinationOverlay.addItem(oi_destination);

    // Add the overlay to the map
    map.getOverlays().add(destinationOverlay);

    // Zoom and pan the map to show all overlays
    map.getController().zoomToSpan(
            new BoundingBox(currentLocation, destination));
}

这是错误消息:

  selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
  Activity com.wat.nav.pedastriannav.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{417e3700 V.E..... R......D 0,0-513,144} that was originally added here
  android.view.WindowLeaked: Activity com.wat.nav.pedastriannav.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{417e3700 V.E..... R......D 0,0-513,144} that was originally added here
    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:443)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:235)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    at android.app.Dialog.show(Dialog.java:282)
    at com.wat.nav.pedastriannav.MainActivity$SearchDestinationTask.onPreExecute(MainActivity.java:341)
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
    at android.os.AsyncTask.execute(AsyncTask.java:534)
    at com.wat.nav.pedastriannav.MainActivity$2.onClick(MainActivity.java:117)
    at android.view.View.performClick(View.java:4377)
    at android.view.View$PerformClick.run(View.java:18044)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5306)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在不知道确切错误消息的情况下,很难猜到。但是,我敢打赌,由于Toast方法中的doInBackground类语句,会发生错误。您无法从后台线程调用UI线程(show()类的Toast)上的方法调用。

答案 1 :(得分:0)

使用Progressdialog或toast时,需要在主线程中使用

在显示或取消进度时添加此行

getBaseContext().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            dialog.setMessage(message);
            dialog.show();
        }
    });