我的項目中遇到了一個大問題。Android Studio - java.lang.OutOfMemoryError問題
我上傳了18張照片到項目中,此後此錯誤開始。
我試着尋找解決方案,但它並沒有幫助。
我發現dexOptions { 增量真 javaMaxHeapSize「4G」 }
因爲它似乎它的工作對大多數人我不知道如果我在正確的位置寫的。
我試圖把它寫在我的gradle上,但它一直在問我一個插件。
如果有人能幫助我,我會很感激。
添加日誌。
java.lang.OutOfMemoryError: Failed to allocate a 111314956 byte allocation with 12232500 free bytes and 78MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageButton.<init>(ImageButton.java:84)
at android.widget.ImageButton.<init>(ImageButton.java:80)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:59)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:55)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at childernsapp.raz.com.learning.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我上點擊代碼,因爲它崩潰所有的時間
ImageButton oneBtn= (ImageButton) findViewById(R.id.One);
oneBtn.setOnClickListener(this);
ImageButton twoBtn = (ImageButton) findViewById(R.id.Two);
twoBtn.setOnClickListener(this);
ImageButton threeBtn = (ImageButton) findViewById(R.id.Three);
threeBtn.setOnClickListener(this);
ImageButton fourBtn = (ImageButton) findViewById(R.id.Four);
fourBtn.setOnClickListener(this);
ImageButton fiveBtn = (ImageButton) findViewById(R.id.Five);
fiveBtn.setOnClickListener(this);
ImageButton sixBtn = (ImageButton) findViewById(R.id.Sixth);
sixBtn.setOnClickListener(this);
ImageButton sevenBtn = (ImageButton) findViewById(R.id.Seven);
sevenBtn.setOnClickListener(this);
ImageButton eightBtn = (ImageButton) findViewById(R.id.Eight);
eightBtn.setOnClickListener(this);
ImageButton nineBtn = (ImageButton) findViewById(R.id.Nine);
nineBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ImageView myImg = (ImageView) findViewById(R.id.myNum);
if (v.getId() == R.id.One)
{
myImg.setImageResource(R.drawable.one);
}
我的XML`
<ImageButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/Two"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/numtwo"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/Three"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/numthree"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_alignBottom="@+id/Two"
android:background="#ffffff" />
<ImageView
android:layout_width="200dp"
android:layout_height="150dp"
android:id="@+id/myNum"
android:contentDescription="myImg"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Four"
android:src="@drawable/numfour"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_below="@+id/One"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Five"
android:layout_below="@+id/One"
android:layout_centerHorizontal="true"
android:src="@drawable/numfive"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Sixth"
android:layout_alignTop="@+id/Five"
android:layout_alignLeft="@+id/Three"
android:layout_alignStart="@+id/Three"
android:src="@drawable/numsixth"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Seven"
android:layout_above="@+id/myNum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/numseven"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Eight"
android:layout_alignBottom="@+id/Seven"
android:layout_centerHorizontal="true"
android:src="@drawable/numeight"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Nine"
android:layout_alignTop="@+id/Eight"
android:layout_alignLeft="@+id/Sixth"
android:layout_alignStart="@+id/Sixth"
android:src="@drawable/numnine"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#ffffff"/>
`
請加上要價較高。 –
等一下,你有沒有與android-studio或android-app有關的問題? – JohnnyAW
兄弟.. 18圖像應該會導致您的應用程序崩潰..這些圖像大小是什麼?你如何以及在哪裏使用它們? – JannGabriel