当前位置: 首页 > 知识库问答 >
问题:

Android看法膨胀异常:二进制XML文件行#2:在android上inflating类片段时出错。应用程序。活动线程。性能启动活动

扈昀
2023-03-14

我有两天的时间研究这个问题,并尝试了所有的解决方案,但没有任何效果。以下是我的代码:

LogCat:

12-27 19:12:13.950: D/AndroidRuntime(30835): Shutting down VM
12-27 19:12:13.950: W/dalvikvm(30835): threadid=1: thread exiting with uncaught exception (group=0x40ef62a0)
12-27 19:12:13.955: E/AndroidRuntime(30835): FATAL EXCEPTION: main
12-27 19:12:13.955: E/AndroidRuntime(30835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.mystatus/com.me.mystatus.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-27 19:12:13.955: E/AndroidRuntime(30835):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.os.Looper.loop(Looper.java:137)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.ActivityThread.main(ActivityThread.java:4898)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at java.lang.reflect.Method.invokeNative(Native Method)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at java.lang.reflect.Method.invoke(Method.java:511)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at dalvik.system.NativeStart.main(Native Method)
12-27 19:12:13.955: E/AndroidRuntime(30835): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.Activity.setContentView(Activity.java:1924)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at com.me.mystatus.MainActivity.onCreate(MainActivity.java:12)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.Activity.performCreate(Activity.java:5206)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-27 19:12:13.955: E/AndroidRuntime(30835):    ... 11 more
12-27 19:12:13.955: E/AndroidRuntime(30835): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.Fragment.instantiate(Fragment.java:584)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.Fragment.instantiate(Fragment.java:552)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.app.Activity.onCreateView(Activity.java:4849)
12-27 19:12:13.955: E/AndroidRuntime(30835):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-27 19:12:13.955: E/AndroidRuntime(30835):    ... 20 more

以下是我的主要活动。Java语言

程序包com。我mystatus;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity  {

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

活动_Main。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.SupportMapFragment"/>

显示xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.mystatus"
    android:versionCode="1"
    android:versionName="1.0" >
    <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"/>

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.me.mystatus.MainActivity"
            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="myvalue"/>
    </application>

</manifest>

请帮忙。

共有3个答案

龙俊德
2023-03-14

只是为了记录在案。

别忘了在你的AndroidManifest中包含。xml

 <meta-data android:name="com.google.android.geo.API_KEY"
  android:value="KEY" />
岳朝
2023-03-14

使用类而不是android: name

android:name=“com.google.android.gms.maps.SupportMapFragment”/

应更改为

类="com.google.android.gms.maps.支持MapFraank"/

确保文件路径正确,因为在编译过程中,eclipse不会检查路径。

其他参考资料在此链接上。

姜飞飙
2023-03-14

您的SupportMapFragmenr无法识别,请确保已将google play services库作为项目添加到您的工作区中,并添加到您的项目库中(右键单击项目-

 类似资料: