【问题标题】:Error inflating imageview in scrollview android在滚动视图android中膨胀imageview时出错
【发布时间】:2014-08-25 17:18:15
【问题描述】:

我尝试在我的 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): 致命异常: main 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;
    }

   
}

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

【问题讨论】:

  • 任何回复请想法
  • 尝试设置android:src="@drawable/aboutscroll"并删除android:background
  • 认为你我改变了但总是相同的 pb 第 10 行中的相同错误
  • 你能发布完整的堆栈跟踪吗?
  • 好的,我有这个堆栈跟踪:无法启动活动 ComponentInfo {} android 异常:Binary xml line #10: error inflating class android.widget.imageview

标签: imageview scrollview


【解决方案1】:

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

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

最后,您可以尝试在您的应用中找到一个处理图像相关事物的库(例如 Picasso)。

【讨论】:

  • 我找到了这个解决方案,但它对我不起作用stackoverflow.com/questions/4990682/…
  • 这不是真正的解决方案,因为您的非常大的图像仍然会一次性加载到内存中。
  • 可能是如果我使用的是真正的设备而不是模拟器,pb 就会消失
  • 不会。请使用指南。
  • 想你我用 bitmapdrawable 对代码做了同样的改变,现在我可以滚动小于 1900 像素的图像
【解决方案2】:

我遇到了这个问题,花了一些时间拔头发。

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

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

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2014-12-14
    相关资源
    最近更新 更多