在scrollview android中输出imageview时出错

时间:2014-08-25 17:18:15

标签: imageview scrollview

我尝试在我的Android应用中使用滚动图像,但在我的调试器中出现此错误:

Binary xml error in line 10 :inflating android widget imageview

这是xml代码

<?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>

这是堆栈跟踪:

  

08-26 11:43:58.102:E / AndroidRuntime(24553):致命异常:主08-26 11:43:58.102:

     

E / AndroidRuntime(24553):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.albir / com.example.albir.About}:android.view.InflateException:二进制XML文件行#10:扩展类android.widget.ImageView

时出错

我使用了指南并且我做了相同的更改所以现在我只能滚动具有700 * 1900像素的图像而不是这么大的调试器显示此跟踪:异常内存不足这个代码:

<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>

java类

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;
    }


}

我想知道我是否可以修改此代码以显示更大的图像

2 个答案:

答案 0 :(得分:0)

您加载的图片似乎太大,导致某种内存不足异常。您可以尝试使用较小的图像并将它们放入适当的文件夹(drawable-hdpi,drawable-mdpi等)。

如果这不适合您,请仔细阅读以下指南,以便最好地了解如何在Android中处理大型位图: http://developer.android.com/training/displaying-bitmaps/index.html

最后,您可以尝试找到一个图书馆(如Picasso)来处理您应用中与图片相关的内容。

答案 1 :(得分:0)

我有这个问题,花了一些时间把头发拉出来。

最终是由于我试图加载的图像被意外复制到 drawable-en-rIE (特定于语言环境)文件夹而不是顶级 drawable 文件夹。

只需将源图像移动到 drawable 文件夹即可解决问题。