无法在Activity中实例化com.google.android.gms.maps.MapFragment片段

时间:2016-06-23 10:23:23

标签: android android-fragments google-maps-android-api-2

(编辑)

TabClubActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.e("First", "First");

    setContentView(R.layout.tab_club_layout);  ---> 140th error line in logcat

    Log.e("Second", "Second");

    MapFragment fragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
    map = fragment.getMap();
    fragment.onResume();

    ................

  }

正如您在下面的日志中看到的那样,它正在打印" First"日志声明而不是打印"秒"日志声明。

所以我确定布局的问题。

堆栈跟踪:

06-23 06:05:21.476: E/First(11700): First


06-23 04:54:35.492: E/AndroidRuntime(9841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.steve/com.app.steve.TabClubActivity}: android.view.InflateException: Binary XML file line #175: Error inflating class fragment
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2122)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:756)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.widget.TabHost.setCurrentTab(TabHost.java:420)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:161)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:549)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.View.performClick(View.java:4756)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.View$PerformClick.run(View.java:19749)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.os.Handler.handleCallback(Handler.java:739)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.os.Looper.loop(Looper.java:135)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.ActivityThread.main(ActivityThread.java:5221)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at java.lang.reflect.Method.invoke(Native Method)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at java.lang.reflect.Method.invoke(Method.java:372)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
06-23 04:54:35.492: E/AndroidRuntime(9841): Caused by: android.view.InflateException: Binary XML file line #175: Error inflating class fragment
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Activity.setContentView(Activity.java:2144)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at com.app.steve.TabClubActivity.onCreate(TabClubActivity.java:140)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Activity.performCreate(Activity.java:5933)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
06-23 04:54:35.492: E/AndroidRuntime(9841):     ... 17 more
06-23 04:54:35.492: E/AndroidRuntime(9841): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Fragment.instantiate(Fragment.java:618)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Fragment.instantiate(Fragment.java:582)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Activity.onCreateView(Activity.java:5282)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
06-23 04:54:35.492: E/AndroidRuntime(9841):     ... 32 more
06-23 04:54:35.492: E/AndroidRuntime(9841): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.app.steve-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
06-23 04:54:35.492: E/AndroidRuntime(9841):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
06-23 04:54:35.492: E/AndroidRuntime(9841):     at android.app.Fragment.instantiate(Fragment.java:604)
06-23 04:54:35.492: E/AndroidRuntime(9841):     ... 36 more
06-23 04:54:35.492: E/AndroidRuntime(9841):     Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
06-23 04:54:35.492: E/AndroidRuntime(9841):         at java.lang.Class.classForName(Native Method)
06-23 04:54:35.492: E/AndroidRuntime(9841):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
06-23 04:54:35.492: E/AndroidRuntime(9841):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
06-23 04:54:35.492: E/AndroidRuntime(9841):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
06-23 04:54:35.492: E/AndroidRuntime(9841):         ... 38 more
06-23 04:54:35.492: E/AndroidRuntime(9841):     Caused by: java.lang.NoClassDefFou

tab_club_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/layout_common_ash"
    android:orientation="vertical" >

..................
..................

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <include
            android:id="@+id/add_club_layout"
            layout="@layout/club_add_layout"
            android:visibility="gone" />

        <include
            android:id="@+id/search_club_layout"
            layout="@layout/club_search_layout" />
    </LinearLayout>

</LinearLayout>

在下面的布局中,我使用 mapFragment

club_search_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/layout_common_ash"
    android:orientation="vertical" >

  ....................
  ....................

    <RelativeLayout
        android:id="@+id/rl_club_search_card_flip_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_club_search_top"
        android:background="#FFFFFF" >

        <RelativeLayout
            android:id="@+id/rl_club_search_card_flip_front"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_main_activity"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:clickable="true"
            android:onClick="onCardClick" >

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.MapFragment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_club_search_card_flip_back"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:clickable="true"
            android:onClick="onCardClick"
            android:visibility="gone" >

            <ListView
                android:id="@+id/lv_club_search_card_flip"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:background="#FFFFFF" >
            </ListView>

            <TextView
                android:id="@+id/tv_club_search_card_flip_no_records"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/club_search_card_flip_no_records"
                android:textColor="#000000"
                android:layout_centerInParent="true"
                android:textSize="12sp"
                android:visibility="gone" />
        </RelativeLayout>
    </RelativeLayout>

我正在使用谷歌地图库修订版29。

我在TabClubActivity中使用适配器类。这意味着我使用适配器类作为内部类。

我正在使用Eclipse版本21.我尝试了很多SO帖子。但是无法解决问题。

3 个答案:

答案 0 :(得分:0)

您的活动是否导入 android.app.fragment

如果是这样,请尝试使用 android.v4.app.fragment

稍后,您需要使用 getSupportFragmentManager 而不是 getFragment

答案 1 :(得分:0)

getFragmentManager()更改为getSupportFragmentManager()。此异常,因为您使用的是imports android.app.Fragment。替换为导入android.v4.app.Fragment

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.e("First", "First");

    setContentView(R.layout.tab_club_layout);  ---> 140th error line in logcat

    Log.e("Second", "Second"); 

    MapFragment fragment = ((MapFragment)getSupportFragmentManager().findFragmentById(R.id.map));
    map = fragment.getMap();
    fragment.onResume();

    ................ 

  }

答案 2 :(得分:0)

我花了几天时间。但是无法在Eclipse工具中解决这个问题。然后我尝试使用Android工作室。

地图工作正常。问题只在工作室解决了。在eclipse中很多时候谷歌地图库问题发生在我身上。但是当我搬到工作室时,我的代码工作正常。

由于使用eclipse工具和sdk更改了新操作系统,导致出现错误。可能是Google地图库问题。