android.view.Inflate Exception,模拟器说应用程序意外停止了

时间:2013-03-12 17:42:02

标签: android

我是Android的新手,并通过查看android开发人员教程第一次使用谷歌地图v2。得到例外,不知道出了什么问题。有人可以帮助我吗?

LOG CAT

03-12 22:00:01.678: D/AndroidRuntime(278): Shutting down VM
03-12 22:00:01.678: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception      (group=0x4001d800)
03-12 22:00:01.758: E/AndroidRuntime(278): FATAL EXCEPTION: main
03-12 22:00:01.758: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start      activity      ComponentInfo{com.sameer.example.maps_example/com.sameer.example.maps_example.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
03-12 22:00:01.758: E/AndroidRuntime(278):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 22:00:01.758: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 22:00:01.758: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
03-12 22:00:01.758: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-12 22:00:01.758: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-12 22:00:01.758: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
03-12 22:00:01.758: E/AndroidRuntime(278): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
03-12 22:00:01.758: E/AndroidRuntime(278):  at   android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-12 22:00:01.758: E/AndroidRuntime(278):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.Activity.setContentView(Activity.java:1647)
03-12 22:00:01.758: E/AndroidRuntime(278):  at com.sameer.example.maps_example.MainActivity.onCreate(MainActivity.java:11)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-12 22:00:01.758: E/AndroidRuntime(278):  ... 11 more
03-12 22:00:01.758: E/AndroidRuntime(278): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.sameer.example.maps_example-2.apk]
03-12 22:00:01.758: E/AndroidRuntime(278):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
03-12 22:00:01.758: E/AndroidRuntime(278):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
03-12 22:00:01.758: E/AndroidRuntime(278):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
03-12 22:00:01.758: E/AndroidRuntime(278):  at  android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
03-12 22:00:01.758: E/AndroidRuntime(278):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-12 22:00:01.758: E/AndroidRuntime(278):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-12 22:00:01.758: E/AndroidRuntime(278):  ... 19 more    

XML布局文件

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment"/>

Java文件

package com.sameer.example.maps_example;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
 }

Android Manifest

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sameer.example.maps_example"
android:versionCode="1"
android:versionName="1.0" >

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

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


<uses-permission android:name="com.sameer.example.maps_example.permission.MAPS_RECEIVE"/>
<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"/>
 <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

1 个答案:

答案 0 :(得分:2)

您正在为API V8编写应用程序,但您正在使用MapFragmentActivity类。 相反,您应该使用android-support-v4库并使用SupportMapFragmentFragmentActvity类。

要将支持库添加到项目,您需要使用SDK Manager下载它。 然后你可以右键单击你的项目并选择Android工具 - &gt; SupportLibrary。

然后从FragmentActivity中取出你的地图活动,并让shure进行导入:

android.support.v4.app.FragmentActivity

在您的XML文件中使用SupportMapFragment类而不是MapFragment,如下所示:

 <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />