[英]App stopped working Android Studio
我可以在項目中放入多少活動? 我做一個項目,現在也有13個活動,當我有5到8個活動時,我的項目效果很好。 什么都沒有,沒有崩潰,現在我有13個活動,當我構建項目並打開它時,歡迎屏幕可以工作,我可以打開一個,兩個或三個其他活動,但是在打開4個或5個活動時,我的應用程序停止工作並崩潰。 我認為內存不足,但是我不明白為什么會發生這種情況,因為我已經調整了所有位圖的大小並對其進行了壓縮,並且在startActivity()
之后開始新的活動時,我輸入了finish()
。
這是nocnizivotActivity.java中的一個Java代碼
package teslicinfo.teslic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class nocnizivotActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nocnizivot);
//tnt
LinearLayout lyt_tnt =(LinearLayout)findViewById(R.id.lyt_tnt);
lyt_tnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "tnt");
startActivity(i);
finish();
}
});
//moja kafanica
LinearLayout lyt_mojakafanica =(LinearLayout)findViewById(R.id.lyt_mojakafanica);
lyt_mojakafanica.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "mojakafanica");
startActivity(i);
finish();
}
});
//kaktus
LinearLayout lyt_kaktus =(LinearLayout)findViewById(R.id.lyt_kaktus);
lyt_kaktus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "kaktus");
startActivity(i);
finish();
}
});
//caffex
LinearLayout lyt_caffex =(LinearLayout)findViewById(R.id.lyt_caffex);
lyt_caffex.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "caffex");
startActivity(i);
finish();
}
});
//faraon
LinearLayout lyt_faraon =(LinearLayout)findViewById(R.id.lyt_faraon);
lyt_faraon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "faraon");
startActivity(i);
finish();
}
});
//lane
LinearLayout lyt_trg =(LinearLayout)findViewById(R.id.lyt_trg);
lyt_trg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "trg");
startActivity(i);
finish();
}
});
//lane
LinearLayout lyt_lane =(LinearLayout)findViewById(R.id.lyt_lane);
lyt_lane.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "lane");
startActivity(i);
finish();
}
});
//kastel
LinearLayout lyt_kastel =(LinearLayout)findViewById(R.id.lyt_kastel);
lyt_kastel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "kastel");
startActivity(i);
finish();
}
});
//cepelin
LinearLayout lyt_cepelin =(LinearLayout)findViewById(R.id.lyt_cepelin);
lyt_cepelin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data", "cepelin");
startActivity(i);
finish();
}
});
LinearLayout lyt_dzungla =(LinearLayout)findViewById(R.id.lyt_dzungla);
lyt_dzungla.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), diskotekeActivity.class);
i.putExtra("data","dzungla");
startActivity(i);
finish();
}
});
ImageView imgnazad_nocnizivot =(ImageView)findViewById(R.id.imgnazad_nocnizivot);
imgnazad_nocnizivot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), pocetakActivity.class);
startActivity(i);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_nocnizivot, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這是nocnizivot_layout.xml代碼
這是我的日志:
11-10 10:49:40.169 2704-2704/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
11-10 10:49:40.169 2704-2704/? I/art﹕ Late-enabling JIT
11-10 10:49:40.169 2704-2704/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
11-10 10:49:40.219 2704-2704/teslicinfo.teslic W/System﹕ ClassLoader referenced unknown path: /data/app/teslicinfo.teslic-2/lib/x86
11-10 10:49:40.547 2704-2717/teslicinfo.teslic I/art﹕ Background partial concurrent mark sweep GC freed 81(3KB) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 1.464ms total 120.600ms
11-10 10:49:40.677 2704-2713/teslicinfo.teslic W/art﹕ Suspending all threads took: 16.632ms
11-10 10:49:40.742 2704-2717/teslicinfo.teslic I/art﹕ Background sticky concurrent mark sweep GC freed 21(864B) AllocSpace objects, 0(0B) LOS objects, 0% free, 21MB/21MB, paused 1.278ms total 117.549ms
11-10 10:49:40.926 2704-2724/teslicinfo.teslic D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-10 10:49:40.972 2704-2704/teslicinfo.teslic D/﹕ HostConnection::get() New Host Connection established 0xab9908d0, tid 2704
11-10 10:49:41.045 2704-2724/teslicinfo.teslic D/﹕ HostConnection::get() New Host Connection established 0xab990dd0, tid 2724
11-10 10:49:41.066 2704-2724/teslicinfo.teslic I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-10 10:49:41.151 2704-2724/teslicinfo.teslic W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-10 10:49:41.151 2704-2724/teslicinfo.teslic W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad92c960, error=EGL_SUCCESS
11-10 10:49:44.407 2704-2724/teslicinfo.teslic W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-10 10:49:44.407 2704-2724/teslicinfo.teslic W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xac13e200, error=EGL_SUCCESS
11-10 10:49:44.893 2704-2724/teslicinfo.teslic E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab9ac980
11-10 10:49:46.143 2704-2717/teslicinfo.teslic W/art﹕ Suspending all threads took: 13.446ms
11-10 10:49:46.147 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.147 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.149 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.149 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.149 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.178 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.178 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 267(24KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 1.195ms total 27.810ms
11-10 10:49:46.182 2704-2704/teslicinfo.teslic I/art﹕ Forcing collection of SoftReferences for 1447KB allocation
11-10 10:49:46.182 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.195 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.195 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 3.038ms total 12.420ms
11-10 10:49:46.198 2704-2704/teslicinfo.teslic W/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1481772 byte allocation with 1096520 free bytes and 1070KB until OOM"
11-10 10:49:46.198 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.199 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.211 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.211 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 0 total 10.985ms
11-10 10:49:46.214 2704-2704/teslicinfo.teslic I/art﹕ Forcing collection of SoftReferences for 1447KB allocation
11-10 10:49:46.214 2704-2704/teslicinfo.teslic I/art﹕ Starting a blocking GC Alloc
11-10 10:49:46.227 2704-2704/teslicinfo.teslic I/art﹕ Clamp target GC heap from 34MB to 32MB
11-10 10:49:46.227 2704-2704/teslicinfo.teslic I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 3% free, 30MB/32MB, paused 1.276ms total 12.712ms
11-10 10:49:46.229 2704-2704/teslicinfo.teslic W/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1481772 byte allocation with 1096520 free bytes and 1070KB until OOM"
11-10 10:49:46.231 2704-2704/teslicinfo.teslic D/skia﹕ --- allocation failed for scaled bitmap
11-10 10:49:46.233 2704-2704/teslicinfo.teslic D/AndroidRuntime﹕ Shutting down VM
11-10 10:49:46.233 2704-2704/teslicinfo.teslic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: teslicinfo.teslic, PID: 2704
java.lang.OutOfMemoryError: Failed to allocate a 1481772 byte allocation with 1096520 free bytes and 1070KB 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.Resources.getDrawable(Resources.java:806)
at android.content.Context.getDrawable(Context.java:458)
at android.widget.ImageView.resolveUri(ImageView.java:811)
at android.widget.ImageView.setImageResource(ImageView.java:418)
at teslicinfo.teslic.diskotekeActivity.diskoteke_dzungla(diskotekeActivity.java:21)
at teslicinfo.teslic.diskotekeActivity.onCreate(diskotekeActivity.java:437)
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)
11-10 10:49:47.991 2704-2704/? I/Process﹕ Sending signal. PID: 2704 SIG: 9
支持AFAIK 13活動,所以我想問題出在其他地方..也許您沒有為您的AVD提供足夠的內存。 無論如何,我覺得請您使用片段。
嘗試這樣做可以幫助您在清單文件中添加此標記。
<application
...
...
android:largeHeap="true"
......
......
</application>
它將為您的應用分配大堆
或者試試這個
可能導致此。 若要創建內存有效的位圖,應使用BitmapOptions
Bitmap bm;
bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),100, 100, true);
mPicture = new ImageView(context);
mPicture.setImageBitmap(bm);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.