在活动映射 xml 中膨胀类片段时出错
Posted
技术标签:
【中文标题】在活动映射 xml 中膨胀类片段时出错【英文标题】:Error Inflating class fragment in activity maps xml 【发布时间】:2016-07-21 06:14:25 【问题描述】:这个项目以前一直在工作。运行应用程序时,我不断收到 InflateException。
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: Process: com.example.ali.googleandroid, PID: 14895
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.ali.googleandroid/com.example.ali.googleandroid.MapsActivity: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class fragment
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 12:16:36.102 14895-14895/com.example.ali.googleandroid E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class fragment
MapsActivity.java:
package com.example.ali.googleandroid;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MapsActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, GoogleMap.OnMyLocationChangeListener, GoogleMap.OnMarkerClickListener
private static final int REQUEST_FINE_LOCATION = 0;
private static int doubleTap =0;
static GoogleMap mMap;
MediaPlayer mp;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
static Location myLocation;
static LocationManager lm;
LatLng latLng;
SupportMapFragment mFragment;
static ArrayList<Double> DataLon;
static ArrayList<Double> DataLatit;
static ArrayList<String> DataFirst_Name;
static ArrayList<String> DataSurname;
public static boolean closeDown = false;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mp = MediaPlayer.create(this, R.raw.two_tone_nav);
loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_FINE_LOCATION);
if (!isGooglePlayServicesAvailable())
finish();
setContentView(R.layout.activity_maps);
mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mFragment.getMap();
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
buildGoogleApiClient();
mGoogleApiClient.connect();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation != null)
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(" You are here!"));
//insert gather data methods
GatherDataLocLogon();
GatherUserLogon();
//*************Add for loop to load all submitted diary entries******************
for (int i = 0; i < SignInScreen.DEntry.size(); i++)
String Lat = SignInScreen.Latit.get(i);
double DoubleLat = Double.parseDouble(Lat);
String Long = SignInScreen.Lon.get(i);
double DoubleLong = Double.parseDouble(Long);
// mMap.setOnMarkerClickListener(this);
LatLng NewLatLng = new LatLng(DoubleLong, DoubleLat);
mMap.addMarker(new MarkerOptions()
.position(new LatLng(DoubleLong, DoubleLat))
.title(SignInScreen.ETitle.get(i).toString())
.snippet(SignInScreen.DEntry.get(i).toString())
);
mMap.moveCamera(CameraUpdateFactory.newLatLng(NewLatLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(8));
activity_maps.xml:
<AbsoluteLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
android:background="#619ec9"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_ tools:context=".MapsActivity"
/>
<ImageButton
android:onClick="CreateDiaryEntry"
android:layout_
android:layout_
android:id="@+id/DiaryEntry"
android:adjustViewBounds="true"
android:padding="20dp"
android:scaleType="fitXY"
android:layout_alignTop="@+id/trackownlocation"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/trackownlocation"
android:src="@drawable/create_entry"
android:background="@drawable/button_state4"
android:layout_alignParentStart="false"
android:layout_x="267dp"
android:layout_y="330dp" />
<ImageButton
android:onClick="TrackOwnLocation"
android:layout_
android:layout_
android:id="@+id/trackownlocation"
android:src="@drawable/own_location"
android:adjustViewBounds="true"
android:padding="20dp"
android:scaleType="fitXY"
android:layout_below="@+id/map"
android:layout_alignParentStart="false"
android:background="@drawable/button_state2"
android:layout_x="9dp"
android:layout_y="429dp" />
<ImageButton
android:layout_
android:layout_
android:id="@+id/help"
android:adjustViewBounds="true"
android:padding="20dp"
android:scaleType="fitXY"
android:src="@drawable/help"
android:background="@drawable/button_state3"
android:layout_alignTop="@+id/trackownlocation"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:layout_x="139dp"
android:layout_y="431dp" />
<ImageButton
android:layout_
android:layout_
android:id="@+id/imageButton"
android:src="@mipmap/settings_icon"
android:adjustViewBounds="true"
android:padding="20dp"
android:scaleType="fitXY"
android:layout_below="@+id/map"
android:layout_alignParentStart="false"
android:background="@drawable/button_state5"
android:layout_x="8dp"
android:layout_y="333dp" />
<ImageButton
android:layout_
android:layout_
android:id="@+id/imageButton2"
android:src="@mipmap/search_icon"
android:adjustViewBounds="true"
android:padding="20dp"
android:scaleType="fitXY"
android:layout_below="@+id/map"
android:layout_alignParentStart="false"
android:background="@drawable/button_state6"
android:layout_x="138dp"
android:layout_y="333dp" />
</AbsoluteLayout>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ali.googleandroid" >
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="23" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="com.example.ali.googleandroid.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.ali.googleandroid.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.location.GPS_ENABLED_CHANGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity android:name=".SignInScreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Entry_Dislplay" >
</activity>
<activity android:name=".Diary_Entry" >
</activity>
<activity android:name=".Recover_Code" >
</activity>
<activity android:name=".SecDialog" >
</activity>
<activity android:name=".Create_A_New_Acc" >
</activity>
<activity android:name=".ThirDialog" >
</activity>
<activity android:name=".Help_Guide" >
</activity>
<activity android:name=".PersDialog" >
</activity>
<activity android:name=".MapsActivity" >
</activity>
<activity android:name=".LogoutDialog" >
</activity>
</application>
</manifest>
build.gradle:
apply plugin: 'com.android.application'
android
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig
applicationId "com.example.ali.googleandroid"
minSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
productFlavors
dependencies
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
【问题讨论】:
你能发布你的清单吗? 绝对布局很久以前就被弃用了。你为什么还在用它? 因为我想@VivekMishra 【参考方案1】:你应该使用片段class="com.google.android.gms.maps.SupportMapFragment"
...而不是android:name="com.google.android.gms.maps.SupportMapFragment"
【讨论】:
【参考方案2】:添加 OnResume 方法并在那里初始化和设置 Map
【讨论】:
【参考方案3】:将此添加到您的清单中:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
【讨论】:
你也可以发build.gradle
吗?以上是关于在活动映射 xml 中膨胀类片段时出错的主要内容,如果未能解决你的问题,请参考以下文章
第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错
android.view.InflateException:二进制 XML 文件:膨胀类片段时出错
android.view.InflateException 膨胀类片段时出错,二进制 XML 文件第 1 行?
Android android.view.InflateException Binary XML 文件第 16 行:膨胀类片段时出错