为什么会出现“ java.lang.RuntimeException:无法启动活动ComponentInfo”错误?

时间:2018-10-25 21:56:24

标签: java android xml

我是android编程的新手! 当我尝试运行我的应用程序时,出现此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pritam.mysecondapplication/com.example.pritam.mysecondapplication.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2750)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6313)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
 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 com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
    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:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.example.pritam.mysecondapplication.MainActivity.onCreate(MainActivity.java:11)
    at android.app.Activity.performCreate(Activity.java:6757)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6313)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 319389708 byte allocation with 8388608 free bytes and 124MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:857)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:708)
    at android.content.res.Resources.loadDrawable(Resources.java:865)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
    at android.view.View.<init>(View.java:4188)
    at android.view.ViewGroup.<init>(ViewGroup.java:578)
    at android.widget.LinearLayout.<init>(LinearLayout.java:211)
    at android.widget.LinearLayout.<init>(LinearLayout.java:207)
    at android.widget.LinearLayout.<init>(LinearLayout.java:203)
        ... 25 more

这是我的布局代码:

    <?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="wrap_content"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@drawable/b1"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textSize="25dp"
        android:layout_marginTop="10dp"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:fontFamily="serif-monospace"
        android:textStyle="bold"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/username"
        android:textColor="@android:color/white"
        android:textSize="25dp"
        android:textStyle="bold"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="25dp"
        android:fontFamily="serif-monospace"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:fontFamily="serif-monospace"
        android:inputType="text"
        android:hint="@string/hint_username"
        android:textColorHint="@color/hint"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password"
        android:textColor="@android:color/white"
        android:textSize="25dp"
        android:textStyle="bold"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="25dp"
        android:fontFamily="serif-monospace"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:fontFamily="serif-monospace"
        android:inputType="textPassword"
        android:hint="@string/hint_password"
        android:textColorHint="@color/hint"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        />

    <Button
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/btn_login"
        android:background="@color/btn"
        android:layout_marginTop="50dp"
        android:fontFamily="serif-monospace"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_gravity="center"/></LinearLayout>

这是我的清单文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pritam.mysecondapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

我的应用程序针对的是棒棒糖5.0(API级别21)
我看到了有关相关问题的其他答案,但实际上并没有帮助我。这些问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

问题在这里:android:background="@drawable/b1。看起来是个很大的位图,它的大小是多少?