我是Android和java编程的初学者。我创建了一个简单的图像滑块应用程序13(12个图像是5 MB大小,1个是91 Kb)图像与前一个和下一个按钮。当我尝试在设备上测试它时,华为荣耀5x,安装屏幕出现,但然后立即消失。它没有安装。 logcat显示outofmemory错误。我不知道如何解决它。这是我的第一个应用程序。我附上了代码。
JAVA代码:
package com.jibran.ejaz.prayertimes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
import android.content.Intent;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ViewFlipper viewFlipper;
Button next;
Button previous;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == next) {
viewFlipper.showNext();
}
else if (v == previous) {
viewFlipper.showPrevious();
}
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.jibran.ejaz.prayertimes.MainActivity">
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView1"
android:src="@drawable/prayertimes"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView2"
android:src="@drawable/capture1"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView3"
android:src="@drawable/capture2"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView4"
android:src="@drawable/capture3"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView5"
android:src="@drawable/capture4"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView6"
android:src="@drawable/capture5"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView7"
android:src="@drawable/capture6"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView8"
android:src="@drawable/capture7"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView9"
android:src="@drawable/capture8"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView10"
android:src="@drawable/capture9"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView11"
android:src="@drawable/capture10"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView12"
android:src="@drawable/capture11"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:id="@+id/imageView13"
android:src="@drawable/capture12"/>
</ViewFlipper>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/next"/>
</RelativeLayout>
logcat的:
02-17 09:49:52.791 954-954/com.jibran.ejaz.prayertimes V/HwPolicyFactory: : success to get AllImpl object and return....
02-17 09:49:52.801 954-954/com.jibran.ejaz.prayertimes V/HwWidgetFactory: : successes to get AllImpl object and return....
02-17 09:49:52.801 954-954/com.jibran.ejaz.prayertimes V/ActivityThread: ActivityThread,callActivityOnCreate
02-17 09:49:52.881 954-954/com.jibran.ejaz.prayertimes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-17 09:49:52.991 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator mControlPoint1x = 0.15, mControlPoint1y = 0.7, mControlPoint2x = 0.2, mControlPoint2y = 0.98
02-17 09:49:52.991 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator mControlPoint1x = 0.6, mControlPoint1y = 0.9, mControlPoint2x = 0.8, mControlPoint2y = 1.0
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@47c49a4
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator mControlPoint1x = 0.15, mControlPoint1y = 0.7, mControlPoint2x = 0.2, mControlPoint2y = 0.98
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes D/CubicBezierInterpolator: CubicBezierInterpolator mControlPoint1x = 0.6, mControlPoint1y = 0.9, mControlPoint2x = 0.8, mControlPoint2y = 1.0
02-17 09:49:53.001 954-954/com.jibran.ejaz.prayertimes V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@33830d
02-17 09:49:53.071 954-954/com.jibran.ejaz.prayertimes I/HwCust: Constructor found for class android.widget.HwCustTextViewImpl
02-17 09:49:53.071 954-954/com.jibran.ejaz.prayertimes D/HwCust: Create obj success use class android.widget.HwCustTextViewImpl
02-17 09:49:53.571 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.571 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.581 954-954/com.jibran.ejaz.prayertimes I/art: Alloc partial concurrent mark sweep GC freed 337(27KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 253us total 10.199ms
02-17 09:49:53.581 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.591 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.601 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 20(12KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 312us total 15.129ms
02-17 09:49:53.601 954-954/com.jibran.ejaz.prayertimes I/art: Forcing collection of SoftReferences for 445MB allocation
02-17 09:49:53.601 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 11(352B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 286us total 14.715ms
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes W/art: Throwing OutOfMemoryError "Failed to allocate a 467251212 byte allocation with 13040548 free bytes and 173MB until OOM"
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.621 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.631 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.631 954-954/com.jibran.ejaz.prayertimes I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 249us total 7.162ms
02-17 09:49:53.631 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.651 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 278us total 14.701ms
02-17 09:49:53.651 954-954/com.jibran.ejaz.prayertimes I/art: Forcing collection of SoftReferences for 445MB allocation
02-17 09:49:53.651 954-954/com.jibran.ejaz.prayertimes I/art: Starting a blocking GC Alloc
02-17 09:49:53.661 954-954/com.jibran.ejaz.prayertimes I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 40% free, 18MB/31MB, paused 266us total 14.733ms
02-17 09:49:53.661 954-954/com.jibran.ejaz.prayertimes W/art: Throwing OutOfMemoryError "Failed to allocate a 467251212 byte allocation with 13040548 free bytes and 173MB until OOM"
02-17 09:49:53.661 954-954/com.jibran.ejaz.prayertimes D/skia: --- allocation failed for scaled bitmap
02-17 09:49:53.681 954-954/com.jibran.ejaz.prayertimes D/AndroidRuntime: Shutting down VM
02-17 09:49:53.731 954-954/com.jibran.ejaz.prayertimes E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jibran.ejaz.prayertimes, PID: 954
java.lang.OutOfMemoryError: Failed to allocate a 467251212 byte allocation with 13040548 free bytes and 173MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:667)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:488)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1082)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2702)
at android.content.res.Resources.loadDrawable(Resources.java:2603)
at android.content.res.HwResources.loadDrawable(HwResources.java:665)
at android.content.res.TypedArray.getDrawable(TypedArray.java:912)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:1008)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1067)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:765)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:723)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:857)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:817)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:434)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.jibran.ejaz.prayertimes.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
02-17 09:49:54.041 954-954/com.jibran.ejaz.prayertimes I/Process: Sending signal. PID: 954 SIG: 9
答案 0 :(得分:0)
swich to picasso liabrary易于实施
compile&#34; com.squareup.picasso:picasso:2.4.0&#34; 在gradle文件中使用它。
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.placeholder(Your Drawable Resource) //this is optional the image to display while the url image is downloading
.error(Your Drawable Resource) //this is also optional if some error has occurred in downloading the image this image would be displayed
.into(imageView);
答案 1 :(得分:0)
由于ImageView
上的图像来源太大而且View
很多。所以你必须选择减小图像大小和重量或使用第三方库。
所以我建议您使用第三方库来管理它的大小,如Glide:
https://github.com/bumptech/glide
粘贴到build.gradle
app-level
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
在运行时加载图片:
Glide.with(this).load(R.drawable.prayertimes).into(imageView1);
...
编辑
虔诚者应该说出你的理由。
答案 2 :(得分:-1)
使用BitmapFactory.Options http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
缩小图像OutOfMemoryError是android中最常见的问题,尤其是在处理位图时。当由于内存空间不足而无法分配对象时,Java虚拟机(JVM)会抛出此错误,并且垃圾收集器也无法释放一些空间。
int imageWidth, imageHeight;
Bitmap result = Bitmap.createScaledBitmap(bitmapPicture,
imageWidth, imageHeight, false);