2017-02-17 142 views
-4

我是Android和java编程的初学者。我创建了一个简单的图像滑块应用程序,其中13个图像(12个图像大小为5 MB,1个大小为91 Kb)带有上一个和下一个按钮。而当我尝试在设备上进行测试时,华为Honor 5x会出现安装屏幕,但会立即消失。它没有被安装。 logcat显示一个outofmemory错误。我不知道如何解决它。这是我的第一个应用程序。我已附上密码。Android Studio立即终止应用程序

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 = [email protected] 
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 = [email protected] 
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

是的。因为你的形象太大了。 – TruongHieu

+1

您需要缩小图像大小。使用picasso或glide加载图像。 – Piyush

+0

阅读此主题http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object –

回答

-1

缩放图像下使用BitmapFactory.Options http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

OutOfM emoryError是android中最常见的问题,尤其是处理位图时。当由于缺少内存空间而无法分配对象并且垃圾收集器无法释放一些空间时,Java虚拟机(JVM)会抛出此错误。

int imageWidth, imageHeight; 

Bitmap result = Bitmap.createScaledBitmap(bitmapPicture, 
         imageWidth, imageHeight, false); 
+0

尝试这样: – Akshay

+0

我不明白如何使用上述功能。所以我尝试使用在线压缩器压缩图像(57 MB到1.99MB),但仍然无法解决我的问题。您能否使用您提到的解决方案编辑我的代码? –

0

SWICH毕加索liabrary其易于实现

编译 “com.squareup.picasso:毕加索:2.4.0” 使用这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); 
+0

我将所有图像从57 MB压缩到1.99 MB,但仍然无法解决我的问题。 –

+0

我觉得你新使用picaso库它很容易不使用位图编码器 –

+0

使imageview给图像视图上面的图像,我张贴 –

0

因为过大的图像源和View很多ImageView的。所以你必须选择减小图像大小和重量或使用第三方库。

因此,我建议你使用第三方库来管理它的大小为你喜欢滑翔:

https://github.com/bumptech/glide

粘贴在build.gradle应用级

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); 
... 

编辑

开发者应该告诉你的理由。