Android 应用程序因 Intent 而崩溃

标签 android android-intent crash

我有两门课:主课和学习课。当我按下 Main 布局上的“学习”按钮时,它应该转到 Study 类,但应用程序却崩溃了。

这是主类代码:

package com.example.geograpp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class Main extends Activity implements OnClickListener {

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

        View studyButton=this.findViewById(R.id.study_button);
        studyButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.study_button: 
                 Intent studyIntent = new Intent(this, Study.class);
                 startActivity(studyIntent);
                 break;
        }
    }
}

这是 Main 的布局:
    <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" 
    android:background="@drawable/mainmenu_background">

    <Button
        android:id="@+id/study_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="94dp"
        android:text="Study" 
        android:textColor="@color/white"
        android:textSize="22sp"
        android:background="@color/black"/>

    <Button
        android:id="@+id/about_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/game_button"
        android:layout_below="@+id/game_button"
        android:layout_marginTop="40dp"
        android:background="@color/black"
        android:text="About"
        android:textColor="@color/white"
        android:textSize="22sp" />

    <Button
        android:id="@+id/game_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/study_button"
        android:layout_below="@+id/study_button"
        android:layout_marginTop="38dp"
        android:background="@color/black"
        android:text="Game"
        android:textColor="@color/white"
        android:textSize="22sp" />

    <Button
        android:id="@+id/exit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/about_button"
        android:layout_alignRight="@+id/study_button"
        android:layout_below="@+id/about_button"
        android:layout_marginTop="42dp"
        android:background="@color/black"
        android:text="Exit"
        android:textColor="@color/white"
        android:textSize="22sp" />

</RelativeLayout>

这是学习类代码:
package com.example.geograpp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Study extends Activity {

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


}

这是研究的布局:
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Study" 
    android:background="@drawable/study_background">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="106dp"
        android:text="Choose a Continent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/worldmap_clickable" />

</RelativeLayout>

这是 list
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.geograpp"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.geograpp.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.geograpp.Study"
            android:label="@string/title_activity_study" >
        </activity>
    </application>

</manifest>

暗恋后的原木猫:
09-04 22:54:17.176: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 49K, 53% free 2551K/5379K, external 1625K/2137K, paused 135ms
09-04 22:54:45.306: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 12K, 53% free 2577K/5379K, external 23321K/25369K, paused 62ms
09-04 22:54:45.396: E/dalvikvm-heap(334): 22216740-byte external allocation too large for this process.
09-04 22:54:45.496: E/GraphicsJNI(334): VM won't let us allocate 22216740 bytes
09-04 22:54:45.506: D/dalvikvm(334): GC_FOR_MALLOC freed <1K, 53% free 2577K/5379K, external 23321K/25369K, paused 35ms
09-04 22:54:45.506: D/skia(334): --- decoder->decode returned false
09-04 22:54:45.506: D/AndroidRuntime(334): Shutting down VM
09-04 22:54:45.506: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-04 22:54:45.538: E/AndroidRuntime(334): FATAL EXCEPTION: main
09-04 22:54:45.538: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geograpp/com.example.geograpp.Study}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-04 22:54:45.538: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
09-04 22:54:45.538: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-04 22:54:45.538: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-04 22:54:45.538: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
09-04 22:54:45.538: E/AndroidRuntime(334):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-04 22:54:45.538: E/AndroidRuntime(334):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.Activity.setContentView(Activity.java:1657)
09-04 22:54:45.538: E/AndroidRuntime(334):  at com.example.geograpp.Study.onCreate(Study.java:12)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-04 22:54:45.538: E/AndroidRuntime(334):  ... 11 more
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: java.lang.reflect.InvocationTargetException
09-04 22:54:45.538: E/AndroidRuntime(334):  at java.lang.reflect.Constructor.constructNative(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-04 22:54:45.538: E/AndroidRuntime(334):  ... 21 more
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.View.<init>(View.java:1951)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.View.<init>(View.java:1899)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.ViewGroup.<init>(ViewGroup.java:286)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)
09-04 22:54:45.538: E/AndroidRuntime(334):  ... 24 more
09-04 22:55:01.926: I/Process(334): Sending signal. PID: 334 SIG: 9

最佳答案

您的应用程序正在崩溃,因为您的应用程序允许的内存限制已超出。解决此问题的唯一方法是减少应用程序的内存占用。我的猜测是您的整个问题与您的学习布局中设置的背景有关。

android:background="@drawable/study_background"

根据我的经验,图像几乎总是导致 OutOfMemory 错误的原因。查看此图像的分辨率并尝试将其缩小到更适合移动设备的尺寸。测试这是否是问题的一种方法是完全删除背景行并运行应用程序。如果您没有看到任何错误,或者您看到的错误与您看到的错误不同,您就知道是图像导致了您的内存问题。

为了解释我是如何得出这个结论的,请更深入地了解堆栈跟踪中的最后一行:
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.View.<init>(View.java:1951)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.View.<init>(View.java:1899)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.view.ViewGroup.<init>(ViewGroup.java:286)
09-04 22:54:45.538: E/AndroidRuntime(334):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)

如果您在对 RelativeLayout 的构造函数调用期间查看它,并且在尝试解码位图时内存不足。在堆栈跟踪的较高位置,您会看到它导致 xml 膨胀错误。这意味着当 Android 系统试图将您的 xml 转换为 java 对象并将其绘制在屏幕上时,就会出现问题。您解释说,当您的代码试图移动到学习 Activity 时,您看到了这个问题,这表明问题存在于学习 Activity 的 xml 中的某个位置。您的学习 Activity xml 只有一个带有背景设置的 RelativeLayout,表明这是问题所在。

最后,我想指出您还在 ImageView 上设置了一个位图,它也可能需要更适合移动设备的大小。
    android:src="@drawable/worldmap_clickable"

关于Android 应用程序因 Intent 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18626149/

相关文章:

android - 广播 protected Intent 时出现 SecurityException

crash - Tomcat在以下3条消息后被停止:收到无效的消息,签名为18245

python - Boost.Python : __init__ accepting None argument

android - 在 android 中提高 Intent 的成本

java - 应用程序在启动时崩溃

android - 使用 Constraints 而不是 maxWidth 属性来限制宽度

android - 单击具有最新更新的 EditText 应用程序崩溃 (gradle 4.4 - android studio 3.1)

android - 父使用功能 android.hardware.location

android - 修改位图非常慢...还有其他方法吗?

安卓拦截 Intent