谷歌地图v2 - 错误膨胀类片段

时间:2014-08-21 19:39:13

标签: java android google-maps android-fragments

我正在尝试在我的片段中使用Google Map。我收到一个错误,说它无法使类片段膨胀。我不确定我在这里做错了什么。有任何想法吗?这是我的代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MoviesFragment extends Fragment {

private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_movies,
            container, false);

    // create map
    map = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    return rootView;

}

}

这是我的布局:

<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"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/map"
    android:name="info.androidhive.tabsswipe.MoviesFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

logcat的:

08-21 15:46:27.290: E/AndroidRuntime(4042): FATAL EXCEPTION: main
08-21 15:46:27.290: E/AndroidRuntime(4042): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at info.androidhive.tabsswipe.MoviesFragment.onCreateView(MoviesFragment.java:20)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at     android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.View.measure(View.java:15855)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.View.measure(View.java:15855)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.View.measure(View.java:15855)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2451)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.View.measure(View.java:15855)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.Choreographer.doFrame(Choreographer.java:532)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.os.Handler.handleCallback(Handler.java:730)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.os.Looper.loop(Looper.java:137)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.app.ActivityThread.main(ActivityThread.java:5289)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at java.lang.reflect.Method.invoke(Method.java:525)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at dalvik.system.NativeStart.main(Native Method)
08-21 15:46:27.290: E/AndroidRuntime(4042): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
08-21 15:46:27.290: E/AndroidRuntime(4042):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

1 个答案:

答案 0 :(得分:0)

在xml中,写下这个 -

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

在Java类文件中,写下这个 -

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

因为,你正在使用v4.app.Fragment。因此,您必须使用getSupportFragmentManager()