我创建了一个应用程序,可以将google v2地图成功查看到片段中;我看到了地图。
如果我尝试添加此代码:
MapView mapView = (MapView) inflatedView .findViewById(R.id.map);
我收到此错误
android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView
我的文件Fragment5.java是:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment5 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View inflatedView = inflater.inflate(R.layout.fragment_5, container,false);
// This next line is the problem
MapView mapView = (MapView) inflatedView .findViewById(R.id.map);
return inflatedView;
}
}
我的布局文件是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xxxxxxx.yyyyyyyy.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/section_label"
android:text="text title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
</RelativeLayout>
详细错误:
02-24 21:54:37.657: E/AndroidRuntime(19225): FATAL EXCEPTION: main
02-24 21:54:37.657: E/AndroidRuntime(19225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxxx.yyyyyyyy/com.xxxxxxx.yyyyyyyy.MainActivity}: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.os.Looper.loop(Looper.java:137)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.ActivityThread.main(ActivityThread.java:4921)
02-24 21:54:37.657: E/AndroidRuntime(19225): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:54:37.657: E/AndroidRuntime(19225): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 21:54:37.657: E/AndroidRuntime(19225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-24 21:54:37.657: E/AndroidRuntime(19225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
02-24 21:54:37.657: E/AndroidRuntime(19225): at dalvik.system.NativeStart.main(Native Method)
02-24 21:54:37.657: E/AndroidRuntime(19225): Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView
02-24 21:54:37.657: E/AndroidRuntime(19225): at com.xxxxxxx.yyyyyyyy.Fragment5.onCreateView(Fragment5.java:32)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.Activity.performStart(Activity.java:5216)
02-24 21:54:37.657: E/AndroidRuntime(19225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
02-24 21:54:37.657: E/AndroidRuntime(19225): ... 11 more
有人可以告诉他问题在哪里?
非常非常非常感谢: - )
答案 0 :(得分:1)
你不应该写:
inflatedView.findViewById();
相反,你应该写getView().findViewById();
另外,我建议使用此代码来设置MapView:
public class Fragment5 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_5, container,false);
return inflatedView;
}
private void setUpMapIfNeeded() {
MapsInitializer.initialize(getActivity());
if (googleMap == null) {
googleMap = ((MapView) getView().findViewById(R.id.store_map_frag)).getMap();
if (googleMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setMyLocationEnabled(true);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Here is where you get the view and display it in the fragment
mapView = (MapView) getView().findViewById(R.id.store_map_frag);
mapView.onCreate(savedInstanceState);
setUpMapIfNeeded();
}
}
如果您还没有使用NavigationDrawer设置片段本身,那么您应该调用FragmentManager和后续代码(FragmentTransaction,编辑器等)来添加片段,例如 android_Muncher 在他的回答中陈述。
答案 1 :(得分:1)
尝试在oncreate中使用这样的supportmapfragment来膨胀地图。当然,您的活动必须扩展FragmentActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_support_map_fragment);
FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
GoogleMap supportMap = supportmapfragment.getMap();
}