简体   繁体   English

Google Maps v2-错误放大类片段

[英]Google Maps v2 - Error inflating class fragment

I am trying to get a Google Map to work within my fragment. 我正在尝试使Google Map在我的片段中工作。 I get an error saying it could not inflate the class fragment. 我收到一条错误消息,说它无法使类片段膨胀。 I'm not sure what I am doing wrong here. 我不确定我在做什么错。 Any ideas? 有任何想法吗? Here is my code: 这是我的代码:

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;

}

}

And here is my layout: 这是我的布局:

<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: 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)

In xml, write this- 在xml中,编写以下内容-

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

In Java class file, write this- 在Java类文件中,编写以下内容-

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

Its because, you are using v4.app.Fragment . 这是因为,您正在使用v4.app.Fragment So, you have to use getSupportFragmentManager() . 因此,您必须使用getSupportFragmentManager()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM