Issue
I am new to Android development and have a problem with commit my Fragment. When the Async Task is finished I try to commit the Fragment from the OnPost Method, but The App destroys. If I commit the Fragment normally from the MapsActivity.class everything works fine.
@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
Solution
I guess in MapsActivity
you start an async task by calling GetDirectionsData
(that extends from AsyncTask), when the task completed, you return result to
MapsActivity` to update UI.
Root cause: These lines of code make your app crash
MapsActivity mapsActivity = new MapsActivity();
mapsActivity.a = String.valueOf(distancetotal);
mapsActivity.c = String.valueOf(umwegint);
mapsActivity.updateUi();
Why: In Android you do not allowed to create an activity instance, the system will create for you and call onCreate
method to notify you the activity has been created. When you create a new instance of MapsActivity
and call updateUi
method, the system cannot find the activity (because the activity created by you not the system) anywhere and they think the activity might be destroy.
Solution: Add these lines of code in GetDirectionsData
class
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
}
}
In MapsActivity class
DirectionsData directionsData = new GetDirectionsData(this);
directionsData.execute();
Answered By - Son Truong
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.