我想在我的Android应用程序中获取片段视图上的地图。尝试打开此视图时,应用程序崩溃了。到目前为止,我确实设法使用相同的视图处理活动,但我需要将其放在片段中。
这是片段类:
public class NearMapFragment extends Fragment {
private GoogleMap theMap;
@SuppressWarnings("unused")
private int shopIcon;
private Marker vitalis;
private Marker siege;
private ImageButton close;
public NearMapFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_near_map, container, false);
// Check status of Google Play Services
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
// Check Google Play Service Available
try {
if (status != ConnectionResult.SUCCESS) {
}
} catch (Exception e) {
Log.e("Error: GooglePlayServiceUtil: ", "" + e);
}
shopIcon = R.drawable.icoadresse;
if(theMap==null){
//map not instantiated yet
}
theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
if(theMap != null){
//ok - proceed
}
theMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
updatePlaces();
return rootView;
}
private void updatePlaces(){
double lat = 46.581256;
double lon = 0.379868;
String name = "Position 1";
String address = "Poitiers";
LatLng POS1 = new LatLng(lat, lon);
siege = theMap.addMarker(new MarkerOptions()
.position(VITALIS_SIEGE)
.title(name)
.icon(BitmapDescriptorFactory.fromResource(shopIcon))
.snippet(address));
double lat2 = 46.580862;
double lon2 = 0.341258;
String name2 = "Position 2";
String address2 = "Poitiers";
LatLng POS2 = new LatLng(lat2, lon2);
vitalis = theMap.addMarker(new MarkerOptions()
.position(POS2)
.title(name2)
.icon(BitmapDescriptorFactory.fromResource(shopIcon))
.snippet(address2));
theMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(46.574826, 0.352488), 13.0f), 400, null);
}
}
日志:
02-24 09:19:41.596: E/AndroidRuntime(4959): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-24 09:19:41.596: E/AndroidRuntime(4959): at com.begital.vitalis.NearMapFragment.onCreateView(NearMapFragment.java:36)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.Fragment.performCreateView(Fragment.java:1700)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.BackStackRecord.run(BackStackRecord.java:684)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.os.Handler.handleCallback(Handler.java:733)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.os.Looper.loop(Looper.java:137)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-24 09:19:41.596: E/AndroidRuntime(4959): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 09:19:41.596: E/AndroidRuntime(4959): at java.lang.reflect.Method.invoke(Method.java:515)
02-24 09:19:41.596: E/AndroidRuntime(4959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-24 09:19:41.596: E/AndroidRuntime(4959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-24 09:19:41.596: E/AndroidRuntime(4959): at dalvik.system.NativeStart.main(Native Method)
02-24 09:19:41.596: E/AndroidRuntime(4959): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
02-24 09:19:41.596: E/AndroidRuntime(4959): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
答案 0 :(得分:0)
更改此
theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
到
theMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.the_map)).getMap();
同样在xml中 改变
android:name="com.google.android.gms.maps.MapFragment"
到
class="com.google.android.gms.maps.SupportMapFragment"
并使用
扩展您的课程public class NearMapFragment extends FragmentActivity{
}
答案 1 :(得分:0)
问题就在这一行
View rootView = inflater.inflate(R.layout.fragment_near_map, container, false);
使用
container = (ViewGroup) inflater.inflate(R.layout.fragment_near_map, null);