Error inflating imageview in scrollview android

2019-09-03 18:52发布

问题:

I tried to have a scrolling image in my android app but have this error in my debugger:

Binary xml error in line 10 :inflating android widget imageview

This the xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgabout">
 <ScrollView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  >
   <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"
     android:background="@drawable/aboutscroll" />   
 </ScrollView>

</LinearLayout>

And this is the stack trace:

08-26 11:43:58.102: E/AndroidRuntime(24553): FATAL EXCEPTION: main 08-26 11:43:58.102:

E/AndroidRuntime(24553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.albir/com.example.albir.About}: android.view.InflateException: Binary XML file line #10: Error inflating class android.widget.ImageView

i used the guidelines and i maked same changes so now i can scroll only image that had 700*1900 px rather than this large the debuguer show this trace :exception out of memory this the code :

<HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </ScrollView>
  </HorizontalScroll>

the java class

public class About extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.about);
        ImageView iv = (ImageView) findViewById(R.id.imageView1);


        Resources res = getResources();
        BitmapDrawable bitmap = (BitmapDrawable) res.
             getDrawable(R.drawable.aboutscroll);
        int imgW = bitmap.getIntrinsicWidth();
        int imgH = bitmap.getIntrinsicHeight();

        iv.setImageDrawable(bitmap);

        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) iv.getLayoutParams();
        params.width = imgW;
        params.height = imgH;
    }


}

i want to know if i can modify this code to show more large image

回答1:

It seems as though the image you're loading is too large and is causing some sort of out of memory exception. You could try to use smaller images and put them into the appropriate folders (drawable-hdpi, drawable-mdpi etc).

If this isn't suitable for you, please read through the following guides to best understand how you should be handling large bitmaps in Android: http://developer.android.com/training/displaying-bitmaps/index.html

Finally, you could try to find a library (like Picasso) that handles image relates things in your app..



回答2:

I had this issue and spent some time pulling my hair out.

It ended up being due to the image I was trying to load was accidentally copied into the drawable-en-rIE (a locale-specific) folder instead of the top-level drawable folder.

Simply moving the source image into the drawable folder solved the issue for me.