简体   繁体   English

应用停止运行Android Studio

[英]App stopped working Android Studio

How many activities can I put in my project how many pictures? 我可以在项目中放入多少活动? I make one project and have now 13 activities also and when I have between 5 and 8 activities, my project works wonderfully. 我做一个项目,现在也有13个活动,当我有5到8个活动时,我的项目效果很好。 Nothing stacks, nothing crashes and now I have 13 activities and when I build my project and open it, the welcome screen works and I can open one, two or three other activities but on 4 or 5 activities open and my app stops working and crashes. 什么都没有,没有崩溃,现在我有13个活动,当我构建项目并打开它时,欢迎屏幕可以工作,我可以打开一个,两个或三个其他活动,但是在打开4个或5个活动时,我的应用程序停止工作并崩溃。 I think it is out of memory, but I don't understand why it happens because I have all of my bitmaps resized and compressed and when I start a new activity after startActivity() I put finish() . 我认为内存不足,但是我不明白为什么会发生这种情况,因为我已经调整了所有位图的大小并对其进行了压缩,并且在startActivity()之后开始新的活动时,我输入了finish()

Here is one java code from nocnizivotActivity.java 这是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);
}
}

and here is the nocnizivot_layout.xml code 是nocnizivot_layout.xml代码

And this is my logcat: 这是我的日志:

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 activies are supported so i guess the problem is somewhere else..maybe you did not give enough ram to your AVD. 支持AFAIK 13活动,所以我想问题出在其他地方..也许您没有为您的AVD提供足够的内存。 Anyway i feel to adivice you to use fragment. 无论如何,我觉得请您使用片段。

Try this may help you add this tag in your manifest file. 尝试这样做可以帮助您在清单文件中添加此标记。

<application
  ...
  ...
  android:largeHeap="true"
  ......
  ......

</application>

it will allocate large heap for your app 它将为您的应用分配大堆

Or try this 或者试试这个

May cause this. 可能导致此。 For creating Bitmaps memory-efficient you should use BitmapOptions 若要创建内存有效的位图,应使用BitmapOptions

Bitmap bm;
bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),100, 100, true);
mPicture = new ImageView(context);
mPicture.setImageBitmap(bm);

check this Strange out of memory issue while loading an image to a Bitmap object 在将图像加载到位图对象时检查此内存不足问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM