MapFragment、片段 - 错误膨胀 Class 片段

MapFragment, Fragment - Error Inflating Class Fragment

你能帮我解决这个问题吗"Error inflating fragment"

我尝试了一些你可以看到的方法:

  1. 将此添加到 XML 片段主体 class="com.google.android.gms.maps.SupportMapFragment"

  2. 添加元数据和清单权限等

  3. 使用 SupportMapFragment 代替 MapFragment

但是所有这些对我都不起作用,我不知道问题出在哪里以及如何解决,请大家帮忙

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout1"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorBackground"
    android:orientation="vertical"
    android:weightSum="1">

    <android.support.v7.widget.Toolbar
        android:id="@+id/actionbarCheckin"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorRedLogin"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:id="@+id/mytext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Check in"
            android:textColor="#ffffff"
            android:textSize="18sp" />

    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/subactionbar"
        android:layout_marginBottom="0.5dp"
        android:layout_marginTop="0.5dp"
        android:background="@color/subactionbar"
        android:orientation="vertical"
        android:padding="5dp">

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvStoreInfoName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sedang memuat informasi toko ..."
                android:textColor="#ffffff"
                android:textSize="15dip"
                android:textStyle="bold"
                android:typeface="sans" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/frameLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <Button
                android:id="@+id/store_checkin_btn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@color/buttonchekin"
                android:onClick="click_btn"
                android:text="Check In" />
        </FrameLayout>
    </LinearLayout>

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

    <FrameLayout
        android:id="@+id/FrameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/subactionbar"
        android:background="#33aacc"
        android:paddingBottom="5dip"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dip"></FrameLayout>

</LinearLayout>

这是我的 Activity 使用 FragmentActivity

    public class StoresCheckinActivity extends FragmentActivity implements OnMapReadyCallback {

    MapController mapController;
    LocationManager locationManager;
    GeoPoint user_location;
    String targetUrl;
    Context context;
    MyLocationOverlay mMyLocationOverlay;
    double lon;
    double lat;
    public String storeid;
    //    ActionBar actionBar;
    StoreInfo storeinfo;
    ProgressDialog progressDialog;
    static final int PROGRESS_DIALOG = 0;

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_checkin);
        context = this.getApplicationContext();
        targetUrl = Config.getEndPointUrl();

        storeinfo = new StoreInfo();

        storeid = this.getIntent().getExtras().getString("id");
        Log.i("ncdebug", "Store ID(checkin): " + storeid);

//        FragmentManager fmanager = getSupportFragmentManager();
//        Fragment fragment = fmanager.findFragmentById(R.id.map);
//        SupportMapFragment mapFrag = (SupportMapFragment)fragment;
//        mapFrag.getMapAsync(this);
//
//        Toolbar toolbar = (Toolbar) findViewById(R.id.actionbarCheckin);
//
        inCaseOffline();

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.map = googleMap;
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
         != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
          != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);
        map.setIndoorEnabled(true);
        map.setBuildingsEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);
    }

    public void inCaseOffline() {
        /*
         * Toast.makeText( context,
         * "Maaf, koneksi internet ke server saat ini tidak tersedia, cobalah beberapa saat lagi"
         * , Toast.LENGTH_LONG).show(); goBackToList();
         */
        StoreHelper sh = new StoreHelper(context);
        Log.i("ncdebug", "Store ID(incaseoffline): " + storeid);
        StoreData sd = sh.getStore(storeid);

        // penyimpanan sementara
        StoreInfo.storeid = sd.store_id;
        StoreInfo.storename = sd.store_name;
        StoreInfo.storeaddress = sd.store_address;
        StoreInfo.storetelephone = sd.store_telephone;
        StoreInfo.storemanager = sd.store_manager;

        Log.i("Data", sd.store_id + "<->" + sd.store_geo_lat + "," + sd.store_geo_long);

        TextView tvname = (TextView) findViewById(R.id.tvStoreInfoName);
        tvname.setText(sd.store_name);

        ProductsHelper helper = new ProductsHelper(context);
        ProductData[] product = helper.getProducts();
        StoreInfo.storeproducts = product;

        PromotionListData[] promo = new PromotionListData[0];
        StoreInfo.storepromotions = promo;

        DisplayCatHelper helperDisplayCat = new DisplayCatHelper(context);
        CategoryData[] displayCat = helperDisplayCat.getCategories();
        StoreInfo.storeDisplayCat = displayCat;

        PromoCatHelper helperPromoCat = new PromoCatHelper(context);
        CategoryData[] promoCat = helperPromoCat.getCategories();
        StoreInfo.storePromoCat = promoCat;

        CompetitorCatHelper helperCompetitorCat = new CompetitorCatHelper(
                context);
        CategoryData[] competitorCat = helperCompetitorCat.getCategories();
        StoreInfo.storeCompetitorCat = competitorCat;
//        add_overlay(StoreInfo.storename, new LatLng(Double.parseDouble(sd.store_geo_lat), Double.parseDouble(sd.store_geo_long)));
//        actionBar.setProgressBarVisibility(View.INVISIBLE);
        // int lt = (int) (row.getDouble("lat") * 1e6);
        // int ln = (int) (row.getDouble("long") * 1e6);
//         add_overlay(row.getString("name"), new GeoPoint(lt, ln));
    }

    public void add_overlay(String nama_toko, LatLng point) {
        set_map();
        Marker marker = map.addMarker(new MarkerOptions().position(point).draggable(false).icon(BitmapDescriptorFactory.fromResource(R.drawable.mstore)));
        marker.setTitle(nama_toko);
        marker.showInfoWindow();
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 15f));
    }

    private void set_map() {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    public void saveToDb(RestData restData) {
        Log.i("eris", "connection failed so save to db");
        RestHelper helper = new RestHelper(context);
        helper.insertRest(restData);
    }

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info == null) {
            return false;
        } else {
            return info.isConnected();
        }
    }

}

这样的日志:

FATAL EXCEPTION: main
Process: com.apps.userinarts.myapplication, PID: 1950
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.userinarts.myapplication/com.apps.userinarts.myapplication.StoresCheckinActivity}: android.view.InflateException: Binary XML file line #75: Binary XML file line #75: Error inflating class fragment
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #75: Binary XML file line #75: Error inflating class fragment
 at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
 at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
 at android.app.Activity.setContentView(Activity.java:2166)
 at com.apps.userinarts.myapplication.StoresCheckinActivity.onCreate(StoresCheckinActivity.java:84)
 at android.app.Activity.performCreate(Activity.java:6237)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #75: Error inflating class fragment
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
 at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
 at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
 at android.app.Activity.setContentView(Activity.java:2166) 
 at com.apps.userinarts.myapplication.StoresCheckinActivity.onCreate(StoresCheckinActivity.java:84) 
 at android.app.Activity.performCreate(Activity.java:6237) 
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
 at maps.y.k.a(Unknown Source)
 at maps.v.c.a(Unknown Source)
 at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
 at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(Unknown Source)
 at vl.onTransact(:com.google.android.gms.DynamiteModulesB:62)
 at android.os.Binder.transact(Binder.java:387)
 at qle.newMapFragmentDelegate(:com.google.android.gms:179)
 at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms:101)
 at qld.onTransact(:com.google.android.gms:62)
 at android.os.Binder.transact(Binder.java:387)
 at com.google.android.gms.maps.internal.zzc$zza$zza.zzag(Unknown Source)
 at com.google.android.gms.maps.SupportMapFragment$zzb.zzbow(Unknown Source)
 at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
 at com.google.android.gms.dynamic.zza.zza(Unknown Source)
 at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
 at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
 at android.support.v4.app.Fragment.onInflate(Fragment.java:1174)
 at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2414)
 at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
 at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:376)
 at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
 at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflat

Gradle 这里:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.apps.userinarts.myapplication"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('library/httpclient-4.2.1.jar')
    compile files('library/apache-mime4j-0.6.1.jar')
    compile 'com.google.android.gms:play-services-maps:9.4.0'
    compile files('library/httpcore-4.2.1.jar')
    compile files('library/httpmime-4.2.1.jar')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    compile 'com.android.support:support-annotations:24.2.0'
    compile files('library/maps.jar')
    compile 'com.google.android.gms:play-services:9.4.0'
}

此处显示:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">

        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".TestPutih"></activity>
        <activity android:name=".TestActionBar"></activity>
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".StoreAddGeoActivity"></activity>
        <activity android:name=".StoreSetGeo"></activity>
        <activity android:name=".ScheduleActivity"></activity>
        <activity android:name=".StoresNearestActivity"></activity>
        <activity android:name=".MessageListActivity"></activity>
        <activity android:name=".StoresCheckinActivity"></activity>

    </application>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/map_apiku" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

您的 manifest.xml

中似乎缺少 com.google.android.geo.API_KEY

需要使用MapFragment

应该是这样的

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="AIza*************************"/>

在您的 android 应用程序标签内的清单中。

您可以从 google 开发者控制台获取 api 密钥: explanation

很抱歉,这是我的错误,问题是我将元标记放在清单中的外面,所以我将它放在清单中的应用程序标记中

感谢老兄的回复

之前:

<manifest>
<application>

  <activity.....></activity>

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

            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
</manifest>

至:

<manifest>
<application>
    <activity android:name=".CobaMapActivity"></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="@string/map_apiku" />

        </application>
</manifest>