2017-02-17 193 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); 
... 

編輯

開發者應該告訴你的理由。