gpt4 book ai didi

java - fragment 中的 Mapbox onStop

转载 作者:太空狗 更新时间:2023-10-29 15:25:30 25 4
gpt4 key购买 nike

我的 fragment 中有一个 mapbox map 。

public class Map extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public MapView mapView;

public Map() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Map.
*/
// TODO: Rename and change types and number of parameters
public static Map newInstance(String param1, String param2) {
Map fragment = new Map();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
Mapbox.getInstance(getContext(), getString(R.string.access_token));
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_map, container, false);

mapView = (MapView) rootView.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);

mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {

mapboxMap.setStyleUrl("mapbox://styles/some_style_url");

}
});

return rootView;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}

@Override
public void onStart() {
super.onStart();
mapView.onStart();
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}

旋转设备时出现错误

04-25 14:37:48.677 27329-27329/com.travip E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.travip, PID: 27329
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.travip/com.travip.MainActivity}: java.lang.IllegalStateException: MapView#onStop() was not called. You must call this method from the parent's {@link Activity#onStop()} or {@link Fragment#onStop()}.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4170)
at android.app.ActivityThread.access$1000(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.IllegalStateException: MapView#onStop() was not called. You must call this method from the parent's {@link Activity#onStop()} or {@link Fragment#onStop()}.
at com.mapbox.mapboxsdk.maps.MapView.onDestroy(MapView.java:279)
at layout.Map.onDestroy(Map.java:163)
at android.support.v4.app.Fragment.performDestroy(Fragment.java:2434)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1442)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:753)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
at android.app.Activity.performStart(Activity.java:6295)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4170) 
at android.app.ActivityThread.access$1000(ActivityThread.java:163) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5585) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

我已经在 fragment 中实现了 onStop() 。或者我应该在 MainActivity(父 Activity )中实现 onStop() 吗?如果是,怎么办?感谢您的建议。

编辑:logcat 说,必须从父 Activity 调用 onStop(),所以我在 MainActivity 中添加了这个

@Override
protected void onStop() {
super.onStop();
Nearby nearby = new Nearby();
nearby.mapView.onStop();
}

这导致了空指针异常,但我认为它可能对您有用。

最佳答案

我终于想出了解决办法。
自 Mapbox 5.x Mapbox.getInstance(getContext(), getString(R.string.access_token)); 必须 从 fragment 所在的 Activity 中调用,并且不在 fragment 的 onCreate 中。所以我把它从我的 fragment 的 onCreate 放在我的 Activity 的 onCreate 中并且它有效。
希望对您有所帮助。

关于java - fragment 中的 Mapbox onStop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43610989/

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