【问题标题】:Error InflateException Android 6.0错误 InflateException Android 6.0
【发布时间】:2015-11-18 01:22:57
【问题描述】:

当我尝试运行我的应用程序时出现错误。我尝试使用导入将类更改为extends Fragmentextends FragmentActivity,但没有奏效。 因为我在我的清单中添加了一些额外的信息,我认为您可能有一些可能导致报告的问题。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="luizugliano.com.br.googlemapsapp" >

<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"/>

<!--Mostrar a localização atual-->
<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="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <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="AIzaSyAjLBXa8xptO-7CRhvhRQMX8zB-85uR7LU"/>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

logcat 错误

FATAL EXCEPTION: main
11-17 22:55:13.475 3005-3005/luizugliano.com.br.googlemapsapp  E/AndroidRuntime: Process: luizugliano.com.br.googlemapsapp, PID: 3005
11-17 22:55:13.475 3005-3005/luizugliano.com.br.googlemapsapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity    ComponentInfo{luizugliano.com.br.googlemapsapp/luizugliano.com.br.googlemapsapp.Ma inActivity}: android.view.InflateException: Binary XML file line #18: Binary XML  file line #12: Error inflating class fragment
11-17 22:55:13.475 3005-3005/luizugliano.com.br.googlemapsapp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-17 22:55:13.475 3005-3005/luizugliano.com.br.googlemapsapp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)

我的 content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<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"/>

</RelativeLayout>

我的 MainActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

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.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import static  com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_YELLOW;
import static com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker;

public class MainActivity extends AppCompatActivity {

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

    try {
        // Loading map
        initilizeMap();


        double latitude = 0;
        double longitude = 0;
        GPSTracker gps;
        gps = new GPSTracker(this);
        if(gps.canGetLocation()){
            latitude=gps.getLatitude();
            longitude=gps.getLongitude();
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude))
                    .title("Hello Maps ");
            marker.icon(
                    defaultMarker(HUE_YELLOW));
            googleMap.addMarker(marker);
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(longitude,
                            latitude)).zoom(1).build();
            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

        }else{
            gps.showSettingsAlert();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**function to load map If map is not created it will create it for you **/

GoogleMap googleMap;
private void initilizeMap() {

    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        // Showing / hiding your current location
        googleMap.setMyLocationEnabled(true);

        // Enable / Disable zooming controls
        googleMap.getUiSettings().setZoomControlsEnabled(true);

        // Enable / Disable my location button
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);

        // Enable / Disable Compass icon
        googleMap.getUiSettings().setCompassEnabled(true);

        // Enable / Disable Rotate gesture
        googleMap.getUiSettings().setRotateGesturesEnabled(true);

        // Enable / Disable zooming functionality
        googleMap.getUiSettings().setZoomGesturesEnabled(true);

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
}

【问题讨论】:

标签: android android-fragments android-studio android-fragmentactivity


【解决方案1】:

我认为您应该使用 android:name="com.google.android.gms.maps.SupportMapFragment" 而不是 android:name="com.google.android.gms.maps.MapFragment",因为您正在使用 SupportLibraries AppCompatActivity。

出于同样的原因,我认为您也应该将getFragmentManager() 替换为getSupportFragmentManager()

在支持库和常规 API 中使用相同的类有时真的很痛苦,因为您始终必须确保使用正确的类。

【讨论】:

  • 除了您提到的更改之外,我还必须将 MapFragment 更改为 SupportMapFragment 但仍然返回相同的错误。更改代码:googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById( R.id.map)).getMap();.
  • 我想你正在使用 android studio。您是否在 build.gradle 中包含了 google-play-services 的依赖项?
  • 是的 - 我包括compile 'com.google.android.gms:play-services:4.2.+'
  • 我刚刚注意到:您将 contentView 设置为 R.layout.activity_main,但您的布局命名为 content_main。这是正确的还是错字?
  • 这里只是复制代码的时候出错了。真的正确content_main。它是否与我的Manifest? 中的某些设置有关?我正在尝试编辑我的问题,输入我的Manifest,但不允许有很多有问题的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 2019-10-06
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多