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

Android.view.inflateexception二进制xml文件第6行错误或inflating类片段[duplicate]

简意
2023-03-14
package info.androidhive.googlemapsv2;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;

import android.os.Bundle;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

/**
 * This demonstrates how to add a tile overlay to a map.
 */
public class MainActivity extends android.support.v4.app.FragmentActivity {

    /** This returns moon tiles. */
    private static final String MOON_MAP_URL_FORMAT =
            "http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";

    private GoogleMap mMap;

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

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();


            }
        }
    }

    private void setUpMap() {
        mMap.setMapType(GoogleMap.MAP_TYPE_NONE);

        TileProvider tileProvider = new UrlTileProvider(256, 256) {
            @Override
            public synchronized URL getTileUrl(int x, int y, int zoom) {
                // The moon tile coordinate system is reversed.  This is not normal.
                int reversedY = (1 << zoom) - y - 1;
                String s = String.format(Locale.US, MOON_MAP_URL_FORMAT, zoom, x, reversedY);
                URL url = null;
                try {
                    url = new URL(s);
                } catch (MalformedURLException e) {
                    throw new AssertionError(e);
                }
                return url;
            }
        };

        mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.androidhive.googlemapsv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- Requires OpenGL ES version 2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBVoQX4aDTd7tg9R_C-XOxgXqWlwR8sDTo" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

logcat

06-18 10:20:32.797: D/AndroidRuntime(19115): Shutting down VM
06-18 10:20:32.797: W/dalvikvm(19115): threadid=1: thread exiting with uncaught exception (group=0x41409930)
06-18 10:20:32.827: E/AndroidRuntime(19115): FATAL EXCEPTION: main
06-18 10:20:32.827: E/AndroidRuntime(19115): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.ActivityThread.access$700(ActivityThread.java:157)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.os.Looper.loop(Looper.java:176)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.ActivityThread.main(ActivityThread.java:5317)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at dalvik.system.NativeStart.main(Native Method)
06-18 10:20:32.827: E/AndroidRuntime(19115): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.Activity.setContentView(Activity.java:1932)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at info.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:29)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.Activity.performCreate(Activity.java:5326)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
06-18 10:20:32.827: E/AndroidRuntime(19115):    ... 11 more
06-18 10:20:32.827: E/AndroidRuntime(19115): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-18 10:20:32.827: E/AndroidRuntime(19115):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
06-18 10:20:32.827: E/AndroidRuntime(19115):    ... 21 more
06-18 10:20:34.419: I/Process(19115): Sending signal. PID: 19115 SIG: 9

共有1个答案

朱自明
2023-03-14

您的minsdk=“12”所以您应该更改此

public class MainActivity extends android.support.v4.app.FragmentActivity

public class MainActivity extends Activity

也要更改这个

  mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
  mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
 类似资料: