我是Android开发的新手,提交Fragment时遇到问题。异步任务完成后,我尝试通过OnPost方法提交片段,但App销毁了。如果我从MapsActivity.class正常提交Fragment,则一切正常。

 @Override
    protected void onPostExecute(String s) {


        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONArray jsonArray = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
            int count = jsonArray.length();


            Log.i("Distance", jsonArray.toString());




            String[] polyline_array = new String[count];
            JSONObject jsonObject2;
            JSONObject jsonObject3;
            for (int i =0;i<count;i++){

                jsonObject2 = jsonArray.getJSONObject(i);

                String polygone = jsonObject2.getJSONObject("polyline").getString("points");


                polyline_array[i] = polygone;

            }

            distancetotal = 0;

            for (int i =0;i<count;i++){
                jsonObject3 = jsonArray.getJSONObject(i);
                String distance = jsonObject3.getJSONObject("distance").getString("value");






                    this.distancetotal += Double.valueOf(distance);




            }





            float umweg =  ((totaldistance/distancetotal)-1)*100;
             umwegint = (int) umweg;




            MapsActivity mapsActivity = new MapsActivity();
            mapsActivity.a = String.valueOf(distancetotal);
            mapsActivity.c = String.valueOf(umwegint);
            mapsActivity.updateUi();

            int count2 = polyline_array.length;
            for (int i = 0; i< count2;i++){

                PolylineOptions options2 = new PolylineOptions();
                options2.color(Color.GREEN);
                options2.width(10);
                options2.addAll(PolyUtil.decode(polyline_array[i])).geodesic(true);



                mMap.addPolyline(options2);






            }





        } catch (JSONException e) {
            e.printStackTrace();
        }


    }


MapsActivity.class

 public class MapsActivity extends FragmentActivity implements  OnMapReadyCallback, View.OnClickListener, RoutingListener{

    public void updateUi(){


            Fragment newfragment = new ResultFragment();
            fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment4, newfragment);
            fragmentTransaction.commit();

        }

}


java.lang.IllegalStateException: Activity has been destroyed
                  at

android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2087)
                      at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
                      at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
                      at taxiapp.com.taxitracker.MapsActivity.updateUi(MapsActivity.java:607)
                      at taxiapp.com.taxitracker.GetDirectionsData.onPostExecute(GetDirectionsData

最佳答案

我猜在MapsActivity中,您可以通过调用GetDirectionsData(从AsyncTask), when the task completed, you return result to MapsActivity`扩展来更新UI)来启动异步任务。

根本原因:这些代码行使您的应用崩溃

MapsActivity mapsActivity = new MapsActivity();
mapsActivity.a = String.valueOf(distancetotal);
mapsActivity.c = String.valueOf(umwegint);
mapsActivity.updateUi();


原因:在Android中,您不允许创建活动实例,系统将为您创建活动实例,并调用onCreate方法通知您活动已创建。当创建MapsActivity的新实例并调用updateUi方法时,系统无法在任何地方找到该活动(因为不是您由系统创建的活动),因此他们认为该活动可能已被破坏。
解决方案:在GetDirectionsData类中添加以下代码行

public class GetDirectionsData extends AsyncTask<String, Void, String> {

    // Add this line
    private WeakReference<MapsActivity> mapsActivityWeakReference;

    // Add this constructor
    public GetDirectionsData(MapsActivity mapsActivity) {
        mapsActivityWeakReference = new WeakReference<>(mapsActivity);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        // Your code
        float umweg =  ((totaldistance/distancetotal)-1)*100;
        umwegint = (int) umweg;

        // Remove these lines of code
        /*MapsActivity mapsActivity = new MapsActivity();
        mapsActivity.a = String.valueOf(distancetotal);
        mapsActivity.c = String.valueOf(umwegint);
        mapsActivity.updateUi();*/

        // Add these lines of code instead
        MapsActivity mapsActivity = mapsActivityWeakReference.get();
        if (mapsActivity != null) {
            mapsActivity.a = String.valueOf(distancetotal);
            mapsActivity.c = String.valueOf(umwegint);
            mapsActivity.updateUi();
        }

        int count2 = polyline_array.length;
        // Your code
    }
}


在MapsActivity类中

DirectionsData directionsData = new GetDirectionsData(this);
directionsData.execute();

08-17 04:10