在scrollview android中膨胀imageview时出错 [英] Error inflating imageview in scrollview android
问题描述
我试图在我的android应用中显示滚动图像,但在调试器中出现此错误:
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
这是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:
08-26 11:43:58.102: E/AndroidRuntime(24553): FATAL EXCEPTION: 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时出错
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
我使用了指南,并且进行了相同的更改,因此现在我只能滚动具有700 * 1900 px的图像,而不是滚动调试器显示如此大的轨迹:
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>
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;
}
}
我想知道我是否可以修改此代码以显示更大的图像
i want to know if i can modify this code to show more large image
推荐答案
似乎您正在加载的图像太大,并导致某种内存不足异常.您可以尝试使用较小的图像并将其放入适当的文件夹(drawable-hdpi,drawable-mdpi等).
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).
如果这不适合您,请通读以下指南以最好地了解您应该如何在Android中处理大型位图: http://developer.android.com/training/displaying-bitmaps/index.html
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
最后,您可以尝试找到一个库(如Picasso)来处理应用程序中与图像相关的内容.
Finally, you could try to find a library (like Picasso) that handles image relates things in your app..
这篇关于在scrollview android中膨胀imageview时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!