2016-11-11 118 views
-1

當我試圖在真實設備中運行代碼時,它會產生錯誤,但它可以在仿真器上運行。 我認爲錯誤與軟件包有關。表面視圖膨脹錯誤

這是錯誤;

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
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,但它嘗試創建位圖或加載太大的圖像。

嘗試將原始位圖資源縮放到更易於管理的大小。