膨胀Google Maps v2片段会导致ClassNotFoundException

时间:2013-02-13 07:46:24

标签: android google-maps google-maps-android-api-2 google-play-services

抱歉,我发布了这个问题。 我提到了很多链接。 但仍然无法解决问题

  1. 我已关联google-play-services_lib服务。
  2. 将该媒体资源设为Google Inc.
  3. 从谷歌控制台获取API_KEY。
  4. 这是我的Manifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.abc.xyz"
        android:versionCode="1"
        android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    
     <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    
    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    
         <uses-library android:name="com.google.android.maps"/>
    
        <activity
            android:name="com.abc.xyz.MapViews"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="removed" />
    
    </application>
    
    </manifest>
    

    这是我的.java文件:

    public class MapViews extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_views);
        }
    }
    

    这是我的map_views.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
           android:name="com.google.android.gms.maps.SupportMapFragment"
           android:layout_width="wrap_content"
           android:layout_height="match_parent" />
    

    添加我收到错误:

    java.lang.RuntimeException: Unable to start activity \
        ComponentInfo{com.abc.xyz/com.abc.xyz.MapViews}: \
        android.view.InflateException: Binary XML file line #2: \
        Error inflating class fragment
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4627)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
    
    Caused by: android.view.InflateException: Binary XML file line #2: \
        Error inflating class fragment
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
     at android.app.Activity.setContentView(Activity.java:1647)
     at com.abc.xyz.MapViews.onCreate(MapViews.java:12)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
     ... 11 more
    
    Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader \
        dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:\
        /data/app/com.abc.xyz-2.apk]
     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
     at android.view.LayoutInflater.createView(LayoutInflater.java:466)
     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
     ... 19 more`
    

2 个答案:

答案 0 :(得分:3)

如果你在旧设备上使用片段(precomb之前),你应该始终从FragmentActivity扩展你的活动。

因此,解决您的问题:将您的MapViews课程更改为FragmentActivity

import android.support.v4.app.FragmentActivity;

public class MapViews extends FragmentActivity {
  ...

更多信息和完整示例: http://android-er.blogspot.co.uk/2012/12/using-supportmapfragment.html

<强>解释

这里的重要例外是:java.lang.ClassNotFoundException: android.view.fragment

原因是在Honeycomb之前的设备上,当Activity尝试加载xml布局时,它会找到它无法理解的关键字fragment,因此它会尝试将其作为一个简单的内置加载查看课程(android.view.fragment)。

如果扩展FragmentActivity,则会覆盖setContentView方法,以便它可以处理fragment关键字。

答案 1 :(得分:0)

你的布局出了问题。试着像这样定义MapView:

<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="Your Maps API Key goes here"
/>