应用程序在打开Activity时崩溃

时间:2017-05-20 17:14:42

标签: java android

你好,那里的程序员! 在一开始我必须说我现在正在学习机器人3天,所以这可能是一些愚蠢的问题,但我依靠你的理解。 我正在尝试创建一个简单的Activity,它将是一种DiceRoller。它工作了一段时间,但突然间,当我甚至进入活动时,它开始崩溃。我无法理解错误 - 这就是为什么我在困扰你。

我的XML代码: http://www.wklej.org/id/3118631/

Java代码: http://www.wklej.org/id/3118642/

不得不从我的角色限制的原因中删除代码。

错误日志:

FATAL EXCEPTION: main
    Process: com.example.daniel.scrolltest3, PID: 24821
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.daniel.scrolltest3/com.example.daniel.scrolltest3.Dices}: android.view.InflateException: Binary XML file line #128: Binary XML file line #128: Error inflating class <unknown>
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
        at android.app.ActivityThread.access$900(ActivityThread.java:157)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5551)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
     Caused by: android.view.InflateException: Binary XML file line #128: Binary XML file line #128: Error inflating class <unknown>
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:401)
        at android.app.Activity.setContentView(Activity.java:2171)
        at com.example.daniel.scrolltest3.Dices.onCreate(Dices.java:76)
        at android.app.Activity.performCreate(Activity.java:6272)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5551) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
     Caused by: android.view.InflateException: Binary XML file line #128: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:401) 
        at android.app.Activity.setContentView(Activity.java:2171) 
        at com.example.daniel.scrolltest3.Dices.onCreate(Dices.java:76) 
        at android.app.Activity.performCreate(Activity.java:6272) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5551) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:619)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:401) 
        at android.app.Activity.setContentView(Activity.java:2171) 
        at com.example.daniel.scrolltest3.Dices.onCreate(Dices.java:76) 
        at android.app.Activity.performCreate(Activity.java:6272) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5551) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 11520012 byte allocation with 7105080 free bytes and 6MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2738)
        at android.content.res.Resources.loadDrawable(Resources.java:2643)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
        at android.widget.ImageView.<init>(ImageView.java:152)
        at android.widget.ImageView.<init>(ImageView.java:140)
        at android.widget.ImageView.<init>(ImageView.java:136)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:401) 
        at android.app.Activity.setContentView(Activity.java:2171) 
        at com.example.daniel.scrolltest3.Dices.onCreate(Dices.java:76) 
        at android.app.Activity.performCreate(Activity.java:6272) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5551) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

在下面的评论中通过farhan patel的建议学习什么是Glide时,我发现了一个现在正在添加的解决方案

机器人:largeHeap = “真”

进入我的清单文件。它现在看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.daniel.scrolltest3">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".StartupMenu"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Karta1"/>
        <activity android:name=".MainMenu"
            android:screenOrientation="portrait"/>
        <activity android:name=".Dices"
            android:screenOrientation="portrait"/>
        <activity android:name=".ListaKart"
            android:screenOrientation="portrait"/>
        <activity android:name=".Karta1_2" />
        <activity android:name=".WebPage"
            android:screenOrientation="portrait"></activity>

    </application>


</manifest>

1 个答案:

答案 0 :(得分:0)

首先,请勿使用fill_parent使用match_parent。大约7年前, fill_parent 重命名为 match_parent 。他们做同样的事情,但不推荐使用 fill_parent 名称。

在我看来,您收到此错误是因为您将所有图片都放入 drawable 文件夹中。此行为会使您的设备重新缩放图像,这会导致分辨率较高的图像出现OutOfMemory错误。

您需要根据屏幕DPI调整所有图片的大小,并将它们放入如下所示的可绘制文件夹中:

  • 抽拉-xxxhdpi
  • 抽拉-xxhdpi
  • 抽拉-xhdpi
  • 抽拉-HDPI
  • 抽拉-MDPI

检查thisthis以获取更多信息。