Android上的异步任务冻结

时间:2011-05-16 23:12:01

标签: android native android-asynctask freeze

我的活动有一些奇怪的问题,我不知道该怎么做......

private Bitmap mBitmap;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    mBitmap = initBitmap();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuItemNew:
            doSomething();
            return true;    
        case R.id.menuItemNative:
            showList();
            return true;

    }

    return(super.onOptionsItemSelected(item));
}

private boolean selected;

private void showList() {
    CharSequence[] items = {"First", "Second"};

    selected = false;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Selection mode");
    builder.setSingleChoiceItems(items, 0, new OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {
            selected = item == 1;
        }
    });
    builder.setPositiveButton("OK", new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            if (selected) {
                new NativeTask().execute();
            }
        }
    });
    Dialog dia = builder.create();
    dia.show();

}

private class NativeTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        Log.i("task", "before Native Call");
        int buffer[] = new int[mBitmap.getWidth() * mBitmap.getHeight()];
        mBitmap.getPixels(buffer, 0, mBitmap.getWidth(), 0, 0, mBitmap.getWidth(), mBitmap.getHeight());
        int[] result = NativeLibrary.process(pixels);
        Log.i("task", "after Native Call");

        return null;
    }

    @Override
    protected void onProgressUpdate(String... progress) {
        Log.i("task", "in progress");
    }

    @Override
    protected void onPostExecute(String a) {

        Log.i("task", "in postExecute");
    }
}

这是我的活动的相关部分。如您所见,我在那里有菜单,如果您点击第二项,则会出现包含2个项目列表的对话框。然后,如果您选择并确认列表中的第二项,则正在执行AsyncTask并调用本机代码中的某些方法。

看起来不错(对我来说),但是有一些问题 - 当我从列表中选择第二项时,本机方法被正确调用...但是Activity冻结了。

日志包含所有三条消息:

  • 任务:在原生呼叫之前
  • 任务:原生呼叫后
  • 任务:在postExecute中

我不知道还有什么需要做的。我错过了什么吗?当我使用AsyncTask时,为什么我的Activity会冻结?问题是我在AsyncTask中使用Bitmap? NativeLibrary.process(int [])返回正确的数组,没有问题....所以为什么?

我会很高兴任何提示!

P.S:我很抱歉我的英语不好

编辑:这是冻结日志

05-17 01:44:53.831: INFO/task(27629): after Natvive Call
05-17 01:44:53.831: INFO/task(27629): in postExecute
05-17 01:44:56.391: DEBUG/dalvikvm(24817): GC_EXPLICIT freed 7 objects / 280 bytes in 67ms
05-17 01:45:05.991: DEBUG/dalvikvm(16702): GC_EXPLICIT freed 103 objects / 3760 bytes in 669ms
05-17 01:45:24.011: DEBUG/dalvikvm(16635): GC_EXPLICIT freed 7 objects / 280 bytes in 690ms
05-17 01:45:27.181: WARN/WindowManager(96): Key dispatching timed out sending to my.test.application/my.test.application.NativeActivity
05-17 01:45:27.181: WARN/WindowManager(96): Previous dispatch state: {{KeyEvent{action=0 code=82 repeat=0 meta=0 scancode=139 mFlags=8} to Window{46518640 my.test.application/my.test.application.NativeActivity paused=false} @ 1305589511983 lw=Window{46518640 my.test.application/my.test.application.NativeActivity paused=false} lb=android.os.BinderProxy@46426770 fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{46518640 my.test.application/my.test.application.NativeActivity paused=false}}}
05-17 01:45:27.181: WARN/WindowManager(96): Current dispatch state: {{KeyEvent{action=1 code=82 repeat=0 meta=0 scancode=139 mFlags=8} to Window{46518640 my.test.application/my.test.application.NativeActivity paused=false} @ 1305589527184 lw=Window{46518640 my.test.application/my.test.application.NativeActivity paused=false} lb=android.os.BinderProxy@46426770 fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{46518640 my.test.application/my.test.application.NativeActivity paused=false}}}
05-17 01:45:27.181: WARN/WindowManager(96): waitedFor: 15000, keyDispatchingTimeout: 15000
05-17 01:45:27.221: ERROR/ActivityManager(96): ANR in my.test.application (my.test.application/.NativeActivity)
05-17 01:45:27.221: ERROR/ActivityManager(96): Reason: keyDispatchingTimedOut
05-17 01:45:27.221: ERROR/ActivityManager(96): Load: 1.73 / 1.48 / 1.71
05-17 01:45:27.221: ERROR/ActivityManager(96): CPU usage from 682640ms to 23ms ago:
05-17 01:45:27.221: ERROR/ActivityManager(96):   system_server: 2% = 1% user + 0% kernel / faults: 14389 minor 3 major
05-17 01:45:27.221: ERROR/ActivityManager(96):   com.htc.album: 1% = 1% user + 0% kernel / faults: 23991 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   logcat: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   akmd: 0% = 0% user + 0% kernel / faults: 828 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   debuggerd: 0% = 0% user + 0% kernel / faults: 49 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   d.process.acore: 0% = 0% user + 0% kernel / faults: 1272 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   gets.SenseClock: 0% = 0% user + 0% kernel / faults: 1212 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   d.process.media: 0% = 0% user + 0% kernel / faults: 285 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   rmClockPlusDock: 0% = 0% user + 0% kernel / faults: 298 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   m.android.phone: 0% = 0% user + 0% kernel / faults: 36 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   harrison.agenda: 0% = 0% user + 0% kernel / faults: 264 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   adbd: 0% = 0% user + 0% kernel / faults: 57 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   LocationService: 0% = 0% user + 0% kernel / faults: 169 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   e.process.gapps: 0% = 0% user + 0% kernel / faults: 225 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   mpz.audioplayer: 0% = 0% user + 0% kernel / faults: 130 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   com.htc.bg: 0% = 0% user + 0% kernel / faults: 91 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   panel_on/0: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   w1_bus_master1: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   zygote: 0% = 0% user + 0% kernel / faults: 342 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   .android.htcime: 0% = 0% user + 0% kernel / faults: 118 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   synaptics_wq: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   events/0: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   suspend: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   ds2784-battery.: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   rild: 0% = 0% user + 0% kernel / faults: 2 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   ancedtaskkiller: 0% = 0% user + 0% kernel / faults: 36 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   ny.lindamanager: 0% = 0% user + 0% kernel / faults: 17 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   com.htc.bgp: 0% = 0% user + 0% kernel / faults: 32 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   ksoftirqd/0: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):   ish.batterylife: 0% = 0% user + 0% kernel / faults: 26 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   com.android.mms: 0% = 0% user + 0% kernel / faults: 16 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):   d.process.acore: 0% = 0% user + 0% kernel / faults: 17 minor
05-17 01:45:27.221: ERROR/ActivityManager(96):  +ik.application: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):  +flush-31:0: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):  +ik.application: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):  -flush-179:0: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):  -flush-7:8: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96):  -flush-31:0: 0% = 0% user + 0% kernel
05-17 01:45:27.221: ERROR/ActivityManager(96): TOTAL: 23% = 20% user + 2% kernel + 0% irq
05-17 01:45:27.491: INFO/Process(96): Sending signal. PID: 27629 SIG: 3
05-17 01:45:27.491: INFO/dalvikvm(27629): threadid=3: reacting to signal 3
05-17 01:45:27.691: INFO/Process(96): Sending signal. PID: 96 SIG: 3
05-17 01:45:27.691: INFO/dalvikvm(96): threadid=3: reacting to signal 3

有时我得到这个:

05-17 01:36:59.841: INFO/task(27583): after NativeCall
05-17 01:36:59.841: INFO/task(27583): in postExecute
05-17 01:36:59.953: INFO/DEBUG(11783): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
05-17 01:36:59.953: INFO/DEBUG(11783): Build fingerprint: 'htc_wwe/htc_bravo/bravo/bravo:2.2/FRF91/293415:user/release-keys'
05-17 01:36:59.953: INFO/DEBUG(11783): pid: 27583, tid: 27583  >>> my.test.application <<<
05-17 01:36:59.953: INFO/DEBUG(11783): signal 11 (SIGSEGV), fault addr ffdbb660
05-17 01:36:59.953: INFO/DEBUG(11783):  r0 00000310  r1 002db970  r2 002db660  r3 afd42328
05-17 01:36:59.953: INFO/DEBUG(11783):  r4 1ff5bf9e  r5 afd4372c  r6 0000181f  r7 ffadfcf1
05-17 01:36:59.953: INFO/DEBUG(11783):  r8 bebb3470  r9 41873c1c  10 41873c04  fp bebb37b8
05-17 01:36:59.953: INFO/DEBUG(11783):  ip ffadfcf0  sp bebb33b8  lr afd0c6b5  pc afd0c192  cpsr 20000030
05-17 01:36:59.953: INFO/DEBUG(11783):  d0  6472656767756265  d1  6f6e5f746e65696e
05-17 01:36:59.953: INFO/DEBUG(11783):  d2  7379732f3a70692e  d3  6d6172662f6d6578
05-17 01:36:59.953: INFO/DEBUG(11783):  d4  72662f6b726f7765  d5  2d6b726f77656d61
05-17 01:36:59.953: INFO/DEBUG(11783):  d6  3a6b70612e736572  d7  0000000100000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d8  000000004635d958  d9  4385800000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d10 00000000437d0000  d11 0000000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d12 0000000000000000  d13 0000000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d14 0000000000000000  d15 0000000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d16 0000000b0000000a  d17 c070100000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d18 4035000000000000  d19 0000000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d20 3ff0000000000000  d21 8000000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d22 c035000000000000  d23 ff00080008000700
05-17 01:36:59.953: INFO/DEBUG(11783):  d24 ff00080008000700  d25 ff00080008000700
05-17 01:36:59.953: INFO/DEBUG(11783):  d26 0100010001000100  d27 0100010001000100
05-17 01:36:59.953: INFO/DEBUG(11783):  d28 0100010001000100  d29 3ff0000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  d30 0000000000000000  d31 3ff0000000000000
05-17 01:36:59.953: INFO/DEBUG(11783):  scr 60000012
05-17 01:37:00.031: INFO/DEBUG(11783):          #00  pc 0000c192  /system/lib/libc.so
05-17 01:37:00.061: INFO/DEBUG(11783):          #01  pc 0000c6b0  /system/lib/libc.so
05-17 01:37:00.061: INFO/DEBUG(11783):          #02  pc 0000cd9e  /system/lib/libc.so
05-17 01:37:00.071: INFO/DEBUG(11783):          #03  pc 00019732  /system/lib/libutils.so
05-17 01:37:00.071: INFO/DEBUG(11783):          #04  pc 00048c9a  /system/lib/libandroid_runtime.so
05-17 01:37:00.071: INFO/DEBUG(11783):          #05  pc 00017034  /system/lib/libdvm.so
05-17 01:37:00.071: INFO/DEBUG(11783):          #06  pc 00045784  /system/lib/libdvm.so
05-17 01:37:00.071: INFO/DEBUG(11783):          #07  pc 0001bfa0  /system/lib/libdvm.so
05-17 01:37:00.071: INFO/DEBUG(11783):          #08  pc 00022c04  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #09  pc 00021aa0  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #10  pc 0005d162  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #11  pc 000659ec  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #12  pc 0001bfa0  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #13  pc 00022c04  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #14  pc 00021aa0  /system/lib/libdvm.so
05-17 01:37:00.081: INFO/DEBUG(11783):          #15  pc 0005cfa0  /system/lib/libdvm.so
05-17 01:37:00.091: INFO/DEBUG(11783):          #16  pc 000480fe  /system/lib/libdvm.so
05-17 01:37:00.091: INFO/DEBUG(11783):          #17  pc 0002ffbc  /system/lib/libandroid_runtime.so
05-17 01:37:00.091: INFO/DEBUG(11783):          #18  pc 000310b8  /system/lib/libandroid_runtime.so
05-17 01:37:00.091: INFO/DEBUG(11783):          #19  pc 00008ca8  /system/bin/app_process
05-17 01:37:00.091: INFO/DEBUG(11783):          #20  pc 0000d430  /system/lib/libc.so
05-17 01:37:00.091: INFO/DEBUG(11783): code around pc:
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c170 c004f8c2 f0436843 f8c00c01 e09ec004 
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c180 0403f040 ea4f6054 f04c04dc 2c1f0701 
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c190 f841604f d81dc00c c138f8df 070ceb03 
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c1a0 0624f107 500cf853 00c4eb06 fa162601 
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c1b0 422cf404 432cd104 400cf843 e0044603 
05-17 01:37:00.091: INFO/DEBUG(11783): code around lr:
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c694 0f41f115 f04fbf88 d80c35ff 350b4927 
05-17 01:37:00.091: INFO/DEBUG(11783): afd0c6a4 0507f025 68431860 4628b12b fc1cf7ff 
05-17 01:37:00.101: INFO/DEBUG(11783): afd0c6b4 28004606 4e21d132 689119a2 d819428d 
05-17 01:37:00.101: INFO/DEBUG(11783): afd0c6c4 69561b4b d9082b0f 60931970 0c01f043 
05-17 01:37:00.101: INFO/DEBUG(11783): afd0c6d4 f8c06150 50c3c004 2300e017 61536093 
05-17 01:37:00.101: INFO/DEBUG(11783): stack:
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3378  afd4372c  /system/lib/libc.so
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb337c  00000000  
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3380  00357588  [heap]
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3384  0000a000  [heap]
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3388  bebb3470  [stack]
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb338c  41873c1c  
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3390  41873c04  
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3394  afd0be7d  /system/lib/libc.so
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb3398  00000000  
05-17 01:37:00.101: INFO/DEBUG(11783):     bebb339c  00000000  
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33a0  00001404  
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33a4  bebb33e8  [stack]
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33a8  fffffff1  
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33ac  bebb33e8  [stack]
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33b0  df002777  
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33b4  e3a070ad  
05-17 01:37:00.111: INFO/DEBUG(11783): #00 bebb33b8  00000308  
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33bc  afd42328  /system/lib/libc.so
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33c0  afd438e4  /system/lib/libc.so
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33c4  afd1040c  /system/lib/libc.so
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33c8  00000003  
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33cc  afd43860  /system/lib/libc.so
05-17 01:37:00.111: INFO/DEBUG(11783):     bebb33d0  00000003  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33d4  afd42328  /system/lib/libc.so
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33d8  00000310  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33dc  00000003  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33e0  00000308  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33e4  afd0c6b5  /system/lib/libc.so
05-17 01:37:00.121: INFO/DEBUG(11783): #01 bebb33e8  432c3dd3  /system/framework/framework-res.apk
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33ec  00000000  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33f0  00000187  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33f4  00298c90  [heap]
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33f8  00000000  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb33fc  00000308  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb3400  00000000  
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb3404  00236320  [heap]
05-17 01:37:00.121: INFO/DEBUG(11783):     bebb3408  00298988  [heap]
05-17 01:37:00.131: INFO/DEBUG(11783):     bebb340c  a8126a18  /system/lib/libutils.so
05-17 01:37:00.131: INFO/DEBUG(11783):     bebb3410  00000308  
05-17 01:37:00.131: INFO/DEBUG(11783):     bebb3414  afd0cda1  /system/lib/libc.so
05-17 01:37:00.861: INFO/ActivityManager(96): Process my.test.application (pid 27583) has died.

在第二种情况下它不会释放但活动崩溃......

编辑:我在Native部分发现奇怪的内存泄漏,当我修复它时,一切正常:)

2 个答案:

答案 0 :(得分:0)

我很困惑,因为NativeTask的声明声明它采用String类型的参数:

new NativeTask().execute(string1);

不是

new NativeTask().execute();

你似乎也没有在doInBackground中使用字符串参数:

String inString= params[0];

希望有所帮助, JAL

答案 1 :(得分:0)

我在原生部分发现了一些内存泄漏。当我修复它时,一切正常。