gpt4 book ai didi

java - NoSaveStateFramelayout 的问题

转载 作者:行者123 更新时间:2023-12-01 04:58:39 27 4
gpt4 key购买 nike

我正在开发一个应用程序,其中我将 Sherlock Actionbar 与 Fragment 集成。有两个选项卡,第一个是 map ,第二个是列表。对于“第一个 map ”选项卡,我想将 MapsForge 开源库基础 map 放入第一个选项卡中。我已从 GIt from Here 获取带有 fragment 的 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)

代码:

map fragment :

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());
}
}
}

最佳答案

根据您提供的链接,您是否有理由放弃他们的 getView() 方法?

@Override
public MapView getView() {
NoSaveStateFrameLayout wrapper = (NoSaveStateFrameLayout)super.getView();
return (MapView)((wrapper != null) ? wrapper.getChildAt(0) : null);
}

关于java - NoSaveStateFramelayout 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684330/

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