gpt4 book ai didi

android - 在 Postexec 上提交 fragment

转载 作者:行者123 更新时间:2023-11-29 23:40:35 26 4
gpt4 key购买 nike

我是 Android 开发新手,提交 fragment 时遇到问题。异步任务完成后,我尝试从 OnPost 方法提交 fragment ,但应用程序销毁了。如果我通常从 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 扩展)开始一个异步任务,当任务完成时,您将结果返回给MapsActivity` 更新 UI。

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

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

原因:在Android中你不允许创建一个activity实例,系统会为你创建并调用onCreate方法来通知你activity已经创建。当您创建一个新的 MapsActivity 实例并调用 updateUi 方法时,系统无法在任何地方找到该 Activity (因为您创建的 Activity 不是系统创建的)并且他们认为该 Activity 可能会被摧毁。

解决方案: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();

关于android - 在 Postexec 上提交 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51907934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com