Surface View Inflate错误

时间:2016-11-11 05:38:51

标签: java android

当我尝试在真实设备中运行代码时,它会出错,但它可以在模拟器上运行。 我认为错误与包有关。

这是错误;

11-11 18:27:57.481 30000-30000/com.lag.duckdodger.duckdodger E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.lag.duckdodger.duckdodger, PID: 30000
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lag.duckdodger.duckdodger/com.lag.duckdodger.duckdodger.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.lag.duckdodger.duckdodger.View.AnimationView
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                            Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.lag.duckdodger.duckdodger.View.AnimationView
                                                                            Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.lag.duckdodger.duckdodger.View.AnimationView
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                               at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                                               at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                               at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                               at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                               at com.lag.duckdodger.duckdodger.MainActivity.onCreate(MainActivity.java:12)
                                                                               at android.app.Activity.performCreate(Activity.java:6664)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                            Caused by: java.lang.OutOfMemoryError: Failed to allocate a 103219212 byte allocation with 12866376 free bytes and 12MB until OOM
                                                                               at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                               at android.graphics.Bitmap.nativeCreate(Native Method)
                                                                               at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
                                                                               at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
                                                                               at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
                                                                               at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:663)
                                                                               at com.lag.duckdodger.duckdodger.AnimationModel.Background.<init>(Background.java:44)
                                                                               at com.lag.duckdodger.duckdodger.View.AnimationView.<init>(AnimationView.java:32)
                                                                               at java.lang.reflect.Constructor.newInstance0(Native Method) 
                                                                               at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
                                                                               at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) 
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) 
                                                                               at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
                                                                               at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                               at com.lag.duckdodger.duckdodger.MainActivity.onCreate(MainActivity.java:12) 
                                                                               at android.app.Activity.performCreate(Activity.java:6664) 
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

以下是这些软件包的屏幕截图: Package Screenshot

这是表面视图;

    package com.lag.duckdodger.duckdodger.View;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.lag.duckdodger.duckdodger.AnimationModel.Background;
import com.lag.duckdodger.duckdodger.AnimationModel.LogModel;
import com.lag.duckdodger.duckdodger.AnimationModel.LogModel2;
import com.lag.duckdodger.duckdodger.AnimationModel.PlayerModel;
import com.lag.duckdodger.duckdodger.AnimationModel.RockModel;

/**
 * Created by FrancisJan on 20/10/2016.
 */

public class AnimationView extends SurfaceView implements SurfaceHolder.Callback{

    private final AnimationThread DuckThread;
    private final Background bg;
    private final PlayerModel pm;
    private final LogModel lm;
    private final RockModel rm;
    private final LogModel2 lm2;

    public AnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
        DuckThread = new AnimationThread(this);
        bg = new Background(context);
        pm = new PlayerModel(context);
        lm = new LogModel(context);
        rm = new RockModel(context);
        lm2 = new LogModel2(context);
    }

这是布局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"
    android:id="@+id/activity_main"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    tools:context="com.lag.duckdodger.duckdodger.MainActivity">

    <view
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.lag.duckdodger.duckdodger.View.AnimationView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/view" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 103219212 byte allocation with 12866376 free bytes and 12MB until OOM
          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
          at android.graphics.Bitmap.nativeCreate(Native Method)
          at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
          at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
          at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
          at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:663)
          at com.lag.duckdodger.duckdodger.AnimationModel.Background.<init>(Background.java:44)
          at com.lag.duckdodger.duckdodger.View.AnimationView.<init>(AnimationView.java:32)

基本上,AnimationView的构造函数正在创建一个Background对象。 Background的构造函数正在尝试创建一个大103MB的位图。那太大了。 (尺寸接近5000x5000像素的图像)您没有向Background显示代码,但它尝试创建位图或加载太大的图像。

尝试将原始位图资源扩展为更易于管理的大小。