android Map错误膨胀类片段

时间:2014-02-05 16:23:16

标签: java android google-maps android-fragments

启动地图应用程序时遇到问题。我已经获得了我的Api Key。我已经配置了一切。但每当我尝试启动此地图应用程序时,它会给我一个InflationException错误。我不知道为什么。我已经检查了一切仍然无法在这里追踪错误。请帮我。

GoogleMapActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

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

public class GoogleMapActivity extends Activity {
GoogleMap map;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// map.setMapType(GoogleMap.MAP_TYPE_NONE);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

错误日志

02-06 00:15:56.597: E/AndroidRuntime(273): FATAL EXCEPTION: main
02-06 00:15:56.597: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemap/com.example.googlemap.GoogleMapActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.os.Looper.loop(Looper.java:123)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 00:15:56.597: E/AndroidRuntime(273):  at java.lang.reflect.Method.invokeNative(Native Method)
02-06 00:15:56.597: E/AndroidRuntime(273):  at java.lang.reflect.Method.invoke(Method.java:521)
02-06 00:15:56.597: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 00:15:56.597: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 00:15:56.597: E/AndroidRuntime(273):  at dalvik.system.NativeStart.main(Native Method)
02-06 00:15:56.597: E/AndroidRuntime(273): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-06 00:15:56.597: E/AndroidRuntime(273):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.Activity.setContentView(Activity.java:1647)
02-06 00:15:56.597: E/AndroidRuntime(273):  at com.example.googlemap.GoogleMapActivity.onCreate(GoogleMapActivity.java:18)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-06 00:15:56.597: E/AndroidRuntime(273):  ... 11 more
02-06 00:15:56.597: E/AndroidRuntime(273): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.googlemap-1.apk]
02-06 00:15:56.597: E/AndroidRuntime(273):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
02-06 00:15:56.597: E/AndroidRuntime(273):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-06 00:15:56.597: E/AndroidRuntime(273):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
02-06 00:15:56.597: E/AndroidRuntime(273):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
02-06 00:15:56.597: E/AndroidRuntime(273):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
        android:name="com.example.googlemap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.googlemap.GoogleMapActivity"
            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.gms.version"

            android:value="@integer/google_play_services_version" />

         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAu12sciArOxuYhDkhNk4tKQ0XStYDall0" />
    </application>

</manifest> 

修改 * 应用FragmentActivity时的错误日志2 *

02-06 01:05:09.674: I/dalvikvm(278): Could not find method com.example.googlemap.GoogleMapActivity.getFragmentManager, referenced from method com.example.googlemap.GoogleMapActivity.onCreate
02-06 01:05:09.674: W/dalvikvm(278): VFY: unable to resolve virtual method 5465: Lcom/example/googlemap/GoogleMapActivity;.getFragmentManager ()Landroid/app/FragmentManager;
02-06 01:05:09.674: D/dalvikvm(278): VFY: replacing opcode 0x6e at 0x0008
02-06 01:05:09.684: D/dalvikvm(278): VFY: dead code 0x000b-0021 in Lcom/example/googlemap/GoogleMapActivity;.onCreate (Landroid/os/Bundle;)V
02-06 01:05:09.804: D/dalvikvm(278): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp
02-06 01:05:09.814: W/dalvikvm(278): VFY: unable to resolve instance field 36
02-06 01:05:09.814: D/dalvikvm(278): VFY: replacing opcode 0x52 at 0x0012
02-06 01:05:09.814: D/dalvikvm(278): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
02-06 01:05:09.864: W/GooglePlayServicesUtil(278): Google Play Store is missing.
02-06 01:05:09.874: W/GooglePlayServicesUtil(278): Google Play Store is missing.
02-06 01:05:09.885: W/GooglePlayServicesUtil(278): Google Play Store is missing.
02-06 01:05:09.894: W/GooglePlayServicesUtil(278): Google Play Store is missing.
02-06 01:05:09.904: W/GooglePlayServicesUtil(278): Google Play Store is missing.
02-06 01:05:09.924: D/AndroidRuntime(278): Shutting down VM
02-06 01:05:09.924: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-06 01:05:09.944: E/AndroidRuntime(278): FATAL EXCEPTION: main
02-06 01:05:09.944: E/AndroidRuntime(278): java.lang.NoSuchMethodError: com.example.googlemap.GoogleMapActivity.getFragmentManager
02-06 01:05:09.944: E/AndroidRuntime(278):  at com.example.googlemap.GoogleMapActivity.onCreate(GoogleMapActivity.java:21)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
02-06 01:05:09.944: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 01:05:09.944: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
02-06 01:05:09.944: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
02-06 01:05:09.944: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 01:05:09.944: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 01:05:09.944: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
02-06 01:05:27.495: I/Process(278): Sending signal. PID: 278 SIG: 9

1 个答案:

答案 0 :(得分:1)

编辑


如果你的min sdk版本是8,你将不得不使用兼容性库。您需要使用android.support.v4.app.Fragment实现您的片段。

将此导入添加到您的活动中:

import android.support.v4.app.FragmentActivity; 

并改变这一点:

public class GoogleMapActivity extends Activity {

到此:

public class GoogleMapActivity extends FragmentActivity {

如果有更多问题,请看看这个:

http://developer.android.com/training/basics/fragments/creating.html