当我 运行 应用时强制关闭

Force close when I run app

我正在尝试构建应用程序来计算平面图形的面积,当我测试这个应用程序时,这个应用程序在启动时强制关闭。

MainActivity.java

package com.amalhanaja.luasbangundatar;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private RelativeLayout persegi, pp, bk, s;

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

        persegi = (RelativeLayout)findViewById(R.id.click_persegi);
        pp = (RelativeLayout)findViewById(R.id.click_persegi_panjang);
        bk = (RelativeLayout)findViewById(R.id.click_belah_ketupat);
        s = (RelativeLayout)findViewById(R.id.click_segitiga);           

        persegi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,LuasPersegi.class);
                startActivity(i);
            }
        });

        pp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,LuasPersegiPanjang.class);
                startActivity(i);
            }
        });

        bk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,LuasBelahKetupat.class);
                startActivity(i);
            }
        });

        s.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,LuasSegitiga.class);
                startActivity(i);
            }
        });

    }
}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@color/primary_material_dark"
    tools:context="com.amalhanaja.luasbangundatar.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pilihan :"
        android:textColor="@android:color/white"
        android:textSize="28sp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:clickable="true"
            android:id="@+id/click_persegi">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/persegi"
                android:id="@+id/gambar_persegi"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/persegi"
                android:gravity="center_horizontal"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/white"
                android:background="#80000000"
                android:textSize="20sp"
                />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:clickable="true"
            android:layout_weight="1"
            android:id="@+id/click_persegi_panjang">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/persegi_panjang"
                android:id="@+id/gambar_persegi_panjang"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/persegi_panjang"
                android:gravity="center_horizontal"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/white"
                android:background="#80000000"
                android:textSize="20sp"
                />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:clickable="true"
            android:id="@+id/click_segitiga">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/segitiga"
                android:id="@+id/gambar_segitiga"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/segitiga"
                android:gravity="center_horizontal"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/white"
                android:background="#80000000"
                android:textSize="20sp"
                />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:clickable="true"
            android:layout_weight="1"
            android:id="@+id/click_belah_ketupat">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/belah_ketupat"
                android:id="@+id/gambar_belah_ketupat"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/belah_ketupat"
                android:gravity="center_horizontal"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:textColor="@android:color/white"
                android:background="#80000000"
                android:textSize="20sp"
                />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

而且我不知道发生了什么,但是 logcat 总是这样显示

> .luasbangundatar/com.amalhanaja.luasbangundatar.MainActivity}: 
android.view.InflateException: Binary XML file line #178: Binary XML file line #178: Error inflating class <unknown>
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5465)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: android.view.InflateException: Binary XML file line #178: Binary XML file line #178: 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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
  at com.amalhanaja.luasbangundatar.MainActivity.onCreate(MainActivity.java:16)
  at android.app.Activity.performCreate(Activity.java:6251)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5465) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: android.view.InflateException: Binary XML file line #178: 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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) 
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) 
  at com.amalhanaja.luasbangundatar.MainActivity.onCreate(MainActivity.java:16) 
  at android.app.Activity.performCreate(Activity.java:6251) 
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) 
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5465) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 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 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) 
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) 
  at com.amalhanaja.luasbangundatar.MainActivity.onCreate(MainActivity.java:16) 
  at android.app.Activity.performCreate(Activity.java:6251) 
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) 
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5465) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 11366924 byte allocation with 2146744 free bytes and 2MB 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:2728)
  at android.content.res.Resources.loadDrawable(Resources.java:2633)
  at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
  at android.widget.ImageView.<init>(ImageView.java:152)
  at android.widget.ImageView.<init>(I

inflate 异常实际上并不是问题所在,而实际上来自布局中的另一个更深层次的问题,该问题随后被包裹在 InflateException 中。一个常见的问题是在尝试扩充加载可绘制资源的图像视图时出现内存不足异常。如果其中一个资源具有高像素分辨率,则会占用大量内存,从而导致膨胀异常。

请在清单 activity 标签中将 Theme 定义为 AppCompat,如下所示 android:theme="@style/Theme.AppCompat.Light.DarkActionBar"

可能是解决崩溃问题,因为你扩展了AppCompatActivity,但我认为你没有将主题设置为AppCompat

试试吧。