我的片段中有一张mapbox地图。
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)
我在片段中实现了onStop()。或者我应该在MainActivity(父活动)中实现onStop()?如果是,怎么样?谢谢你的建议。
修改 logcat说,onStop()必须从父活动中调用,所以我在MainActivity中添加了这个
@Override
protected void onStop() {
super.onStop();
Nearby nearby = new Nearby();
nearby.mapView.onStop();
}
这导致空指针异常,但我认为它可能对你有用。
答案 0 :(得分:-1)
我终于想出了如何解决它
由于Mapbox 5.x Mapbox.getInstance(getContext(), getString(R.string.access_token));
必须从片段所在的活动中调用,而不是在片段的onCreate中调用。所以我把它从我的片段的onCreate里面放在我活动的onCreate中,它起作用了。
我希望这会对你有所帮助。