我想在Android应用程序中相互设置一些PNG图像 并通过单击按钮将其中一个或一些显示在一起 但当我尝试在另一个ImageView上设置一些ImageView时,应用程序崩溃了。
这是我的xml布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#ff0000">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" android:layout_gravity="center_horizontal"
android:background="@drawable/freepizza"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView2" android:src="@drawable/gobalak_kam"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView9" android:background="@drawable/goje_motevaset"/>
它的错误日志代码:
04-30 11:42:07.827: E/AndroidRuntime(3484): FATAL EXCEPTION: main
04-30 11:42:07.827: E/AndroidRuntime(3484): Process: com.kayra.pitza, PID: 3484
04-30 11:42:07.827: E/AndroidRuntime(3484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kayra.pitza/com.kayra.pitza.Kitchen}: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.os.Handler.dispatchMessage(Handler.java:102)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.os.Looper.loop(Looper.java:136)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-30 11:42:07.827: E/AndroidRuntime(3484): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 11:42:07.827: E/AndroidRuntime(3484): at java.lang.reflect.Method.invoke(Method.java:515)
04-30 11:42:07.827: E/AndroidRuntime(3484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-30 11:42:07.827: E/AndroidRuntime(3484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-30 11:42:07.827: E/AndroidRuntime(3484): at dalvik.system.NativeStart.main(Native Method)
04-30 11:42:07.827: E/AndroidRuntime(3484): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-30 11:42:07.827: E/AndroidRuntime(3484): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-30 11:42:07.827: E/AndroidRuntime(3484): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.Activity.setContentView(Activity.java:1929)
04-30 11:42:07.827: E/AndroidRuntime(3484): at com.kayra.pitza.Kitchen.onCreate(Kitchen.java:17)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.Activity.performCreate(Activity.java:5231)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-30 11:42:07.827: E/AndroidRuntime(3484): ... 11 more
04-30 11:42:07.827: E/AndroidRuntime(3484): Caused by: java.lang.reflect.InvocationTargetException
04-30 11:42:07.827: E/AndroidRuntime(3484): at java.lang.reflect.Constructor.constructNative(Native Method)
04-30 11:42:07.827: E/AndroidRuntime(3484): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-30 11:42:07.827: E/AndroidRuntime(3484): ... 24 more
04-30 11:42:07.827: E/AndroidRuntime(3484): Caused by: java.lang.OutOfMemoryError
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.content.res.Resources.loadDrawable(Resources.java:2110)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.widget.ImageView.<init>(ImageView.java:129)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.widget.ImageView.<init>(ImageView.java:119)
04-30 11:42:07.827: E/AndroidRuntime(3484): ... 27 more
我必须这样做,但我对如何做不知道。
请帮帮我 如何在xml中相互设置一些图像?
答案 0 :(得分:0)
04-30 11:42:07.827: E/AndroidRuntime(3484): Caused by: java.lang.OutOfMemoryError
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.content.res.Resources.loadDrawable(Resources.java:2110)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.widget.ImageView.<init>(ImageView.java:129)
04-30 11:42:07.827: E/AndroidRuntime(3484): at android.widget.ImageView.<init>(ImageView.java:119)
你有OOM例外有很多方法可以解决它可能会添加
android:largeHeap="true"
到你的应用程序清单标签。另请阅读:
Loading Large Bitmaps Efficiently
看标准方式。
答案 1 :(得分:0)
创建一个名为drawable-nodpi
的资源文件夹,并将图片放在该文件夹而不是drawable
中,这可能有助于解决您的问题!
答案 2 :(得分:0)
使用相对布局而不是像这样的线性布局
<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:background="#FFFFFF" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageview2"
/>
</RelativeLayout>
希望它会对你有所帮助,谢谢