通过线程打开活动导致崩溃

时间:2015-07-19 13:22:07

标签: android android-intent android-activity progress-bar threadpool

对于活动A和B,我在清单中设置了属性android:noHistory = true。 活动B相当,这就是为什么我在线程中调用意图打开该活动的原因,但问题是加载整个应用程序后只是崩溃和着名的意外错误使用Force close按钮发生。任何解决方案都表示赞赏。

这里有活动A:

public class FirstActivity extends Activity {

    private static int PROGRESSSECONDS = 4;
    private static Handler FirstActivityHandler;
    private ProgressBar pb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        FirstActivityHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                pb.setProgress(msg.arg1);
            }
        };
    }

    public void PlayNowAction(View v) {
        setContentView(R.layout.loadingscreen);
        pb = (ProgressBar) findViewById(R.id.pbLoader);
        pb.setMax(PROGRESSSECONDS);
        pb.setProgress(0);

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int i = 1; i <= PROGRESSSECONDS; i++) {
                        Thread.sleep(1000);
                        Message msg = Message.obtain();
                        msg.arg1 = i;
                        FirstActivityHandler.sendMessage(msg);
                    }

                } catch (InterruptedException e) {

                } finally {
                    Intent in = new Intent(FirstActivity.this,
                            PointsDrawerActivity.class);
                    startActivity(in);
                }
            }
        }).start();

    }
}

这里有活动B:

public class PointsDrawerActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        addReccords(); // this a working method but it takes very long time
    }

}

这是我的LogCat:

07-19 18:22:45.492: D/gralloc_goldfish(1460): Emulator without GPU emulation detected.
07-19 18:22:57.052: D/dalvikvm(1460): GC_EXTERNAL_ALLOC freed 84K, 48% free 2809K/5379K, external 716K/1038K, paused 8ms
07-19 18:23:01.132: D/dalvikvm(1460): GC_EXTERNAL_ALLOC freed 17K, 48% free 2815K/5379K, external 1264K/1776K, paused 9ms
07-19 18:23:01.173: D/dalvikvm(1460): GC_EXTERNAL_ALLOC freed 44K, 49% free 2789K/5379K, external 11634K/13682K, paused 0ms
07-19 18:23:01.213: D/dalvikvm(1460): GC_EXTERNAL_ALLOC freed 16K, 49% free 2775K/5379K, external 22798K/24846K, paused 7ms
07-19 18:23:01.213: E/dalvikvm-heap(1460): 11312112-byte external allocation too large for this process.
07-19 18:23:01.232: D/dalvikvm(1460): GC_FOR_MALLOC freed 0K, 49% free 2775K/5379K, external 22798K/24846K, paused 7ms
07-19 18:23:01.232: E/GraphicsJNI(1460): VM won't let us allocate 11312112 bytes
07-19 18:23:01.232: D/skia(1460): --- decoder->decode returned false
07-19 18:23:01.242: D/AndroidRuntime(1460): Shutting down VM
07-19 18:23:01.242: W/dalvikvm(1460): threadid=1: thread exiting with uncaught exception (group=0xb5fa34f0)
07-19 18:23:01.242: E/AndroidRuntime(1460): FATAL EXCEPTION: main
07-19 18:23:01.242: E/AndroidRuntime(1460): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at com.jutt.dotbot.DotedImage.<init>(DotedImage.java:31)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at com.jutt.dotbot.PointsDrawerActivity.addImagesObject(PointsDrawerActivity.java:274)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at com.jutt.dotbot.PointsDrawerActivity.onCreate(PointsDrawerActivity.java:85)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.os.Looper.loop(Looper.java:130)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at java.lang.reflect.Method.invoke(Method.java:507)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-19 18:23:01.242: E/AndroidRuntime(1460):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我担心实际问题是使用位图。您的应用程序遭受了臭名昭着的OutOfMemoryError,如果您的应用程序与几个位图有任何关系,这注定会发生。有必要处理它们。这是一种方法: OutOfMemoryError resolution