android.view.inflateexception二进制xml文件行#7错误膨胀类片段

时间:2014-06-23 03:28:12

标签: android xml google-maps android-fragments

我是android.i的新手需要将图片从网址加载到标记。在这样做我得到错误" android.view.inflateexception二进制xml文件行#7错误膨胀类片段"

代码 MainActivity

package com.ngshah.goglemapv2withlazyloading;

import java.util.Hashtable;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
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 com.ngshah.goglemapv2withlazyloading.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.FIFOLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class MainActivity extends FragmentActivity {

    private GoogleMap googleMap;
    private final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private final LatLng KIEL = new LatLng(53.551, 9.993);
    private Marker marker;
    private Hashtable<String, String> markers;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

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

        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        initImageLoader();
        markers = new Hashtable<String, String>();
        imageLoader = ImageLoader.getInstance();

        options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_launcher)      //  Display Stub Image
            .showImageForEmptyUri(R.drawable.ic_launcher)   //  If Empty image found
            .cacheInMemory()
            .cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();

        if ( googleMap != null ) {

            googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

            final Marker hamburg = googleMap.addMarker(new MarkerOptions().position(HAMBURG)
                        .title("Hamburg"));
            markers.put(hamburg.getId(), "http://img.india-forums.com/images/100x100/37525-a-still-image-of-akshay-kumar.jpg");

            final Marker kiel = googleMap.addMarker(new MarkerOptions()
                        .position(KIEL)
                        .title("Kiel")
                        .snippet("Kiel is cool")
                        .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));
            markers.put(kiel.getId(), "http://www.yodot.com/images/jpeg-images-sm.png");

            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
    }

    private class CustomInfoWindowAdapter implements InfoWindowAdapter {

        private View view;

        public CustomInfoWindowAdapter() {
            view = getLayoutInflater().inflate(R.layout.custom_info_window,
                    null);
        }

        @Override
        public View getInfoContents(Marker marker) {

            if (MainActivity.this.marker != null
                    && MainActivity.this.marker.isInfoWindowShown()) {
                MainActivity.this.marker.hideInfoWindow();
                MainActivity.this.marker.showInfoWindow();
            }
            return null;
        }

        @Override
        public View getInfoWindow(final Marker marker) {
            MainActivity.this.marker = marker;

            String url = null;

            if (marker.getId() != null && markers != null && markers.size() > 0) {
                if ( markers.get(marker.getId()) != null &&
                        markers.get(marker.getId()) != null) {
                    url = markers.get(marker.getId());
                }
            }
            final ImageView image = ((ImageView) view.findViewById(R.id.badge));

            if (url != null && !url.equalsIgnoreCase("null")
                    && !url.equalsIgnoreCase("")) {
                imageLoader.displayImage(url, image, options,
                        new SimpleImageLoadingListener() {
                            @Override
                            public void onLoadingComplete(String imageUri,
                                    View view, Bitmap loadedImage) {
                                super.onLoadingComplete(imageUri, view,
                                        loadedImage);
                                getInfoContents(marker);
                            }
                        });
            } else {
                image.setImageResource(R.drawable.ic_launcher);
            }

            final String title = marker.getTitle();
            final TextView titleUi = ((TextView) view.findViewById(R.id.title));
            if (title != null) {
                titleUi.setText(title);
            } else {
                titleUi.setText("");
            }

            final String snippet = marker.getSnippet();
            final TextView snippetUi = ((TextView) view
                    .findViewById(R.id.snippet));
            if (snippet != null) {
                snippetUi.setText(snippet);
            } else {
                snippetUi.setText("");
            }

            return view;
        }
    }

    private void initImageLoader() {
        int memoryCacheSize;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            int memClass = ((ActivityManager) 
                    getSystemService(Context.ACTIVITY_SERVICE))
                    .getMemoryClass();
            memoryCacheSize = (memClass / 8) * 1024 * 1024;
        } else {
            memoryCacheSize = 2 * 1024 * 1024;
        }

        final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this).threadPoolSize(5)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .memoryCacheSize(memoryCacheSize)
                .memoryCache(new FIFOLimitedMemoryCache(memoryCacheSize-1000000))
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() 
                .build();

        ImageLoader.getInstance().init(config);
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

</RelativeLayout>

custom_info_window.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_info_bubble"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/badge"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="5dp"
        android:adjustViewBounds="true" >
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#ff000000"
            android:textSize="14dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ff7f7f7f"
            android:textSize="14dp" />
    </LinearLayout>

</LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ngshah.goglemapv2withlazyloading"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.ngshah.googlemapv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.ngshah.googlemapv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my key" />

        <activity
            android:name="com.ngshah.goglemapv2withlazyloading.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" />
    </application>

</manifest>

日志文件

06-23 08:56:33.749: I/Google Maps Android API(15924): Google Play services client version: 4323000
06-23 08:56:33.759: I/Google Maps Android API(15924): Google Play services package version: 4452034
06-23 08:56:33.819: D/AndroidRuntime(15924): Shutting down VM
06-23 08:56:33.819: W/dalvikvm(15924): threadid=1: thread exiting with uncaught exception (group=0x420ae930)
06-23 08:56:33.899: E/AndroidRuntime(15924): FATAL EXCEPTION: main
06-23 08:56:33.899: E/AndroidRuntime(15924): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ngshah.goglemapv2withlazyloading/com.ngshah.goglemapv2withlazyloading.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.ActivityThread.access$700(ActivityThread.java:157)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.os.Looper.loop(Looper.java:176)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.ActivityThread.main(ActivityThread.java:5317)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at java.lang.reflect.Method.invokeNative(Native Method)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at java.lang.reflect.Method.invoke(Method.java:511)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at dalvik.system.NativeStart.main(Native Method)
06-23 08:56:33.899: E/AndroidRuntime(15924): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.Activity.setContentView(Activity.java:1932)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.ngshah.goglemapv2withlazyloading.MainActivity.onCreate(MainActivity.java:45)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.Activity.performCreate(Activity.java:5326)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
06-23 08:56:33.899: E/AndroidRuntime(15924):    ... 11 more
06-23 08:56:33.899: E/AndroidRuntime(15924): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
06-23 08:56:33.899: E/AndroidRuntime(15924): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
06-23 08:56:33.899: E/AndroidRuntime(15924):    at lzv.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at mcg.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at mcg.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at mbi.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at lxn.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at lxm.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at fnb.onTransact(SourceFile:107)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.os.Binder.transact(Binder.java:310)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.google.android.gms.dynamic.a$4.b(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.google.android.gms.dynamic.a.a(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
06-23 08:56:33.899: E/AndroidRuntime(15924):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
06-23 08:56:33.899: E/AndroidRuntime(15924):    ... 21 more
06-23 08:56:36.151: I/Process(15924): Sending signal. PID: 15924 SIG: 9

3 个答案:

答案 0 :(得分:3)

问题出在您的AndroidManifest.xml中,如LogCat异常中此行所述:

Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

如果您将此权限添加到AndroidManifest,它应该可以正常工作

答案 1 :(得分:0)

public void onDestroy()
 {

    super.onDestroy();

    Fragment fragment = (getFragmentManager()
            .findFragmentById(R.id.map));
    FragmentTransaction ft = getActivity().getSupportFragmentManager()
            .beginTransaction();
    ft.remove(fragment);
    ft.commit();
}

在你的片段中编写代码

答案 2 :(得分:0)

修复错误:

在清单中:

<application
        android:largeHeap="true"

在.xml文件中,您必须删除以下代码:

android:src="@drawable..
android:background="@drawable...

因为错误是内存不足错误。您应该检查代码以处理应用程序的内存。