繁体   English   中英

原因:android.view.InflateException:二进制XML文件第9行:错误膨胀了类android.widget.ImageView

[英]Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView

我还没有找到答案。 我刚刚开始在android studio中学习编程,现在遇到了一些麻烦。 我无法解析代码中发生的事情。 我正在从几个月前最后一次编辑的在线教程中学习,视频中的那个家伙根本没有问题。 下面是代码截图。 提前致谢!!

MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener{
    private ImageView mozartImage;
    private ImageView bachImage;

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

        mozartImage = (ImageView) findViewById(R.id.imageViewMozartId);
        bachImage = (ImageView) findViewById(R.id.imageViewBachId);

        mozartImage.setOnClickListener(this);
        bachImage.setOnClickListener(this);

    }

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.imageViewMozartId:

                Intent mozartIntent = new Intent(MainActivity.this, DetailsActivity.class);
                String mozartBio = "Wolfgang Amadeus Mozart was born in Salzburg and showed prodigious ability" +
                        "from his earliest childhood. Already competent on keyboard and violin, he composed from the age of five " +
                        "and performed before European royalty. At 17. he was engaged as a court musician in Salzburg. ";
                mozartIntent.putExtra("mozart", mozartBio);
                mozartIntent.putExtra("name", "Wolfgang Amadeus Mozart");
                startActivity(mozartIntent);

                break;
            case R.id.imageViewBachId:

                Intent bachIntent = new Intent(MainActivity.this, DetailsActivity.class);
                String bachBio = "Johann Sebastian Bach was born in Eisenach, Saxe-Eisenach, into a great musical family. " +
                        "His father, Johann Ambrosius Bach, was the director of the town musicians, " +
                        "and all of his uncles were proffesional musicians. His father taught him to play the violin. ";
                bachIntent.putExtra("bach", bachBio);
                bachIntent.putExtra("name", "Johann Sebastian Bach");
                startActivity(bachIntent);
                break;

        }

    }
}

DetailsActivity

public class DetailsActivity extends Activity {

    private ImageView profileImage;
    private TextView bioText;
    private Bundle extras;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        profileImage = (ImageView) findViewById(R.id.imageViewBioId);
        bioText = (TextView) findViewById(R.id.textViewBioId);

        extras = getIntent().getExtras();

        if (extras != null) {

            String name = extras.getString("name");
            showDetails(name);

        }
    }


    public void showDetails(String nName) {

        Toast.makeText(this, nName, Toast.LENGTH_LONG).show();
        if (nName.equals("mozart")) {

            profileImage.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.mozart));
            bioText.setText(extras.getString("nName"));

        } else if (nName.equals("bach")) {
            profileImage.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bach));
            bioText.setText(extras.getString("nName"));
        }

    }
}

logcat的

07-03 18:29:36.741  12648-12648/? I/art﹕ Late-enabling -Xcheck:jni
07-03 18:29:37.383  12648-12648/showmethebio.myandroidhello.com.showmethebio            I/art﹕ Alloc partial concurrent mark sweep GC freed 83(16KB) AllocSpace objects,     0(0B) LOS objects, 40% free, 7MB/12MB, paused 350us total 49.224ms
07-03 18:29:37.392  12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc concurrent mark sweep GC freed 19(12KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 388us total 7.055ms
07-03 18:29:37.392  12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Forcing collection of SoftReferences for 96MB allocation
07-03 18:29:37.397  12648-12648/showmethebio.myandroidhello.com.showmethebio E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 101654796 byte allocation with 5184004 free bytes and 88MB until OOM"
07-03 18:29:37.408  12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 268us total 6.618ms
07-03 18:29:37.409  12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Forcing collection of SoftReferences for 96MB allocation
07-03 18:29:37.416  12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 300us total 6.963ms
07-03 18:29:37.416  12648-12648/showmethebio.myandroidhello.com.showmethebio E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 101654796 byte allocation with 5184004 free bytes and 88MB until OOM"
07-03 18:29:37.416  12648-12648/showmethebio.myandroidhello.com.showmethebio D/skia﹕ --- allocation failed for scaled bitmap
07-03 18:29:37.417  12648-12648/showmethebio.myandroidhello.com.showmethebio D/AndroidRuntime﹕ Shutting down VM
07-03 18:29:37.418  12648-12648/showmethebio.myandroidhello.com.showmethebio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: showmethebio.myandroidhello.com.showmethebio, PID: 12648
java.lang.RuntimeException: Unable to start activity ComponentInfo{showmethebio.myandroidhello.com.showmethebio/showmethebio.myandroidhello.com.showmethebio.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView

...

Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)

...

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 101654796 byte allocation with 5184004 free bytes and 88MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)    

我真的很陌生,每一个帮助都会帮助我度过难关。 提前致谢!!

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".DetailsActivity"
            android:label="@string/title_activity_details" >
        </activity>
    </application>

</manifest>

Activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#3bffa729">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:contentDescription="@string/mozart_name"
        android:id="@+id/imageViewMozartId"
        android:background="@drawable/mozart"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp" />

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:contentDescription="@string/bach_name"
        android:id="@+id/imageViewBachId"
        android:background="@drawable/bach"
        android:layout_marginBottom="70dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/mozart_name"
        android:id="@+id/textViewMozartId"
        android:layout_below="@+id/imageViewMozartId"
        android:layout_centerHorizontal="true"
        android:textColor="#ffab0d77" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/bach_name"
        android:id="@+id/textViewBachId"
        android:textColor="#ffab0d77"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textViewMozartId"
        android:layout_alignEnd="@+id/textViewMozartId" />
</RelativeLayout>

Activity_details.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="showmethebio.myandroidhello.com.showmethebio.DetailsActivity"
    android:background="#3bffa729">

    <ImageView
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:contentDescription="@string/bio_name"
        android:id="@+id/imageViewBioId"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:text=""
        android:id="@+id/textViewBioId"
        android:layout_below="@+id/imageViewBioId"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

也许您ImageViewid imageViewMozartId它太大(101654796 = 96,95 mebibyte)。

您应使用大小和重量合理的jpegpng图像。

关于图像/位图的某些信息会导致OutofMemory错误。 android开发人员页面建议使用文档中定义的这两种方法。 仔细阅读它一次对我的帮助很大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM