我正在开发一个应用程序,我将 Sherlock Actionbar 与 Fragment 整合在一起。有两个选项卡,第一个是地图,第二个是列表。对于第一个地图选项卡,我想将 MapsForge 开源库基础地图放在第一个选项卡中。我从 Here 获得了带有来自 GIt的片段的 MapsForge 示例,我试图实现它。但它让我出错NoSaveStateFramelayout
错误 ::
12-03 18:48:49.210: E/AndroidRuntime(2779): FATAL EXCEPTION: main
12-03 18:48:49.210: E/AndroidRuntime(2779): java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.jakewharton.android.mapsforge_fragment.MapView
12-03 18:48:49.210: E/AndroidRuntime(2779): at in.wptrafficanalyzer.actionbarsherlocknavtab.MapFragment.getView(MapFragment.java:32)
12-03 18:48:49.210: E/AndroidRuntime(2779): at in.wptrafficanalyzer.actionbarsherlocknavtab.MapFragment.getView(MapFragment.java:1)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.support.v4.app.FragmentPagerAdapter.isViewFromObject(FragmentPagerAdapter.java:147)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.support.v4.view.ViewPager.infoForChild(ViewPager.java:1258)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.support.v4.view.ViewPager.populate(ViewPager.java:1031)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.View.measure(View.java:12775)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.View.measure(View.java:12775)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.View.measure(View.java:12775)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.View.measure(View.java:12775)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
12-03 18:48:49.210: E/AndroidRuntime(2779): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2240)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.View.measure(View.java:12775)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1117)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.os.Looper.loop(Looper.java:137)
12-03 18:48:49.210: E/AndroidRuntime(2779): at android.app.ActivityThread.main(ActivityThread.java:4514)
12-03 18:48:49.210: E/AndroidRuntime(2779): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 18:48:49.210: E/AndroidRuntime(2779): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 18:48:49.210: E/AndroidRuntime(2779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-03 18:48:49.210: E/AndroidRuntime(2779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-03 18:48:49.210: E/AndroidRuntime(2779): at dalvik.system.NativeStart.main(Native Method)
代码:
地图片段:
package in.wptrafficanalyzer.actionbarsherlocknavtab;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.jakewharton.android.mapsforge_fragment.GeoPoint;
import com.jakewharton.android.mapsforge_fragment.MapView;
import com.jakewharton.android.mapsforge_fragment.MapViewMode;
public class MapFragment extends Fragment {
private static final String KEY_LATITUDE_E6 = "MapFragment:LatitudeE6";
private static final String KEY_LONGITUDE_E6 = "MapFragment:LongitudeE6";
private static final String KEY_ZOOM_LEVEL = "MapFragment:ZoomLevel";
private static final String KEY_MAP_FILE = Environment.getExternalStorageDirectory()+ "/great_britain.map";
private final MapViewMode mMapViewMode;
public MapFragment() {
this(MapView.DEFAULT_MAP_VIEW_MODE);
}
public MapFragment(MapViewMode mapViewMode) {
mMapViewMode = mapViewMode;
}
@Override
public MapView getView() {
return (MapView)super.getView();
}
@Override
public MapView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MapView mapView = new MapView(getActivity(), mMapViewMode);
if (savedInstanceState != null)
{
final int latitude = savedInstanceState.getInt(KEY_LATITUDE_E6, Integer.MIN_VALUE);
final int longitude = savedInstanceState.getInt(KEY_LONGITUDE_E6, Integer.MIN_VALUE);
final byte zoom = savedInstanceState.getByte(KEY_ZOOM_LEVEL, Byte.MIN_VALUE);
final String mapFile = savedInstanceState.getString(KEY_MAP_FILE);
if ((mapFile != null) && !mapView.getMapViewMode().requiresInternetConnection()) {
mapView.setMapFileFromParcel(mapFile);
}
if ((latitude != Integer.MIN_VALUE) && (longitude != Integer.MIN_VALUE) && (zoom != Byte.MIN_VALUE)) {
mapView.setCenterAndZoom(new GeoPoint(latitude, longitude), zoom);
}
}
return mapView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
MapView mapView = getView();
if (mapView != null) {
mapView.destroy();
mapView = null;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
MapView mapView = getView();
if (mapView.hasValidCenter()) {
if (!mapView.getMapViewMode().requiresInternetConnection() && (mapView.getMapFile() != null)) {
outState.putString(KEY_MAP_FILE, mapView.getMapFile());
}
GeoPoint center = mapView.getMapCenter();
outState.putInt(KEY_LATITUDE_E6, center.getLatitudeE6());
outState.putInt(KEY_LONGITUDE_E6, center.getLongitudeE6());
outState.putByte(KEY_ZOOM_LEVEL, mapView.getZoomLevel());
}
}
}
更新 ::
package in.wptrafficanalyzer.actionbarsherlocknavtab;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.jakewharton.android.mapsforge_fragment.GeoPoint;
import com.jakewharton.android.mapsforge_fragment.MapView;
import com.jakewharton.android.mapsforge_fragment.MapViewMode;
public class MapFragment extends Fragment {
private static final String KEY_LATITUDE_E6 = "MapFragment:LatitudeE6";
private static final String KEY_LONGITUDE_E6 = "MapFragment:LongitudeE6";
private static final String KEY_ZOOM_LEVEL = "MapFragment:ZoomLevel";
private static final String KEY_MAP_FILE = Environment.getExternalStorageDirectory()+ "/great_britain.map";
private final MapViewMode mMapViewMode;
public MapFragment() {
this(MapView.DEFAULT_MAP_VIEW_MODE);
}
public MapFragment(MapViewMode mapViewMode) {
mMapViewMode = mapViewMode;
}
@Override
public MapView getView() {
NoSaveStateFrameLayout wrapper = (NoSaveStateFrameLayout)super.getView();
return (MapView)((wrapper != null) ? wrapper.getChildAt(0) : null);
}
@Override
public MapView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MapView mapView = new MapView(getActivity(), mMapViewMode);
if (savedInstanceState != null) {
final int latitude = savedInstanceState.getInt(KEY_LATITUDE_E6, Integer.MIN_VALUE);
final int longitude = savedInstanceState.getInt(KEY_LONGITUDE_E6, Integer.MIN_VALUE);
final byte zoom = savedInstanceState.getByte(KEY_ZOOM_LEVEL, Byte.MIN_VALUE);
final String mapFile = savedInstanceState.getString(KEY_MAP_FILE);
if ((mapFile != null) && !mapView.getMapViewMode().requiresInternetConnection()) {
mapView.setMapFileFromParcel(mapFile);
}
if ((latitude != Integer.MIN_VALUE) && (longitude != Integer.MIN_VALUE) && (zoom != Byte.MIN_VALUE)) {
mapView.setCenterAndZoom(new GeoPoint(latitude, longitude), zoom);
}
}
return mapView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
MapView mapView = getView();
if (mapView != null) {
mapView.destroy();
mapView = null;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
MapView mapView = getView();
if (mapView.hasValidCenter()) {
if (!mapView.getMapViewMode().requiresInternetConnection() && (mapView.getMapFile() != null)) {
outState.putString(KEY_MAP_FILE, mapView.getMapFile());
}
GeoPoint center = mapView.getMapCenter();
outState.putInt(KEY_LATITUDE_E6, center.getLatitudeE6());
outState.putInt(KEY_LONGITUDE_E6, center.getLongitudeE6());
outState.putByte(KEY_ZOOM_LEVEL, mapView.getZoomLevel());
}
}
}