JNI检测到应用程序错误(使用多线程在android中操作位图时发生错误)

时间:2018-09-29 10:31:18

标签: java android multithreading android-bitmap android-8.0-oreo

在使用Java的android studio项目中,我使用Executors( Executors.newFixedThreadPool 将线程数初始化为3)来运行在位图上执行某些处理的线程。

但是有时在logcat中显示此错误并使活动崩溃。(我从每个日志的第一行中删除了时间和包名)

A/zygote: java_vm_ext.cc:504] JNI DETECTED ERROR IN APPLICATION: obj == null
A/zygote: java_vm_ext.cc:504]     in call to GetLongField
A/zygote: java_vm_ext.cc:504]     from void android.graphics.BaseCanvas.nDrawBitmapMatrix(long, android.graphics.Bitmap, long, long)
A/zygote: java_vm_ext.cc:504] "pool-5-thread-1" prio=5 tid=36 Runnable
A/zygote: java_vm_ext.cc:504]   | group="main" sCount=0 dsCount=0 flags=0 obj=0x12f40558 self=0xca63fa00
A/zygote: java_vm_ext.cc:504]   | sysTid=11252 nice=0 cgrp=default sched=0/0 handle=0xc39f5970
A/zygote: java_vm_ext.cc:504]   | state=R schedstat=( 98866144 6027605 20 ) utm=9 stm=0 core=6 HZ=100
E/proccess Bitmaps thread no:2t 2: 20
A/zygote: java_vm_ext.cc:504]   | stack=0xc38f3000-0xc38f5000 stackSize=1038KB
A/zygote: java_vm_ext.cc:504]   | held mutexes= "mutator lock"(shared held)
A/zygote: java_vm_ext.cc:504]   native: #00 pc 002c6aa3  /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+130)
A/zygote: java_vm_ext.cc:504]   native: #01 pc 00359273  /system/lib/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+202)
A/zygote: java_vm_ext.cc:504]   native: #02 pc 00355757  /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMapb+34)
A/zygote: java_vm_ext.cc:504]   native: #03 pc 002308dd  /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+736)
A/zygote: java_vm_ext.cc:504]   native: #04 pc 00230d47  /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortFEPKcS2_z+66)
A/zygote: java_vm_ext.cc:504]   native: #05 pc 0027a0e5  /system/lib/libart.so (_ZN3art3JNI12GetLongFieldEP7_JNIEnvP8_jobjectP9_jfieldID+584)
A/zygote: java_vm_ext.cc:504]   native: #06 pc 000ce38f  /system/lib/libandroid_runtime.so (_ZN7android6bitmap8toBitmapEP7_JNIEnvP8_jobject+14)
A/zygote: java_vm_ext.cc:504]   native: #07 pc 000cda8b  /system/lib/libandroid_runtime.so (???)
A/zygote: java_vm_ext.cc:504]   native: #08 pc 0047b3e3  /system/framework/arm/boot-framework.oat (Java_android_graphics_BaseCanvas_nDrawBitmapMatrix__JLandroid_graphics_Bitmap_2JJ+162)
A/zygote: java_vm_ext.cc:504]   at android.graphics.BaseCanvas.nDrawBitmapMatrix(Native method)
A/zygote: java_vm_ext.cc:504]   at android.graphics.BaseCanvas.drawBitmap(BaseCanvas.java:111)
A/zygote: java_vm_ext.cc:504]   at android.graphics.Canvas.drawBitmap(Canvas.java:1496)
A/zygote: java_vm_ext.cc:504]   at ir.mytoon.app.MyTest.Struct.Operations.compose(Operations.java:206)
A/zygote: java_vm_ext.cc:504]   at ir.mytoon.app.MyTest.Struct.ProccessingDesing.doOperation(ProccessingDesing.java:188)
A/zygote: java_vm_ext.cc:504]   at ir.mytoon.app.MyTest.Struct.ProccessingDesing.makeFrame(ProccessingDesing.java:66)
A/zygote: java_vm_ext.cc:504]   at ir.mytoon.app.MyTest.MVP.Gif.GifProductMaker.ProccessingGif$proccessFrames.run(ProccessingGif.java:336)
A/zygote: java_vm_ext.cc:504]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
A/zygote: java_vm_ext.cc:504]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
A/zygote: java_vm_ext.cc:504]   at java.lang.Thread.run(Thread.java:784)

我的设备的android版本是 android O 如果我使用一个线程,则不会发生。

我认为导致崩溃的代码行是这样的:

canvas.drawBitmap(proccessingBitmap, processingBitmapMatrix, paint);

任何帮助将不胜感激...

1 个答案:

答案 0 :(得分:1)

所以我发现了问题并想分享,可能有帮助!

实际上,在我的代码中的某些地方,我为以后的canvas.drawBitmap函数提供了位图。

因为我将ArrayList用于位图以供以后操作,并且 Arraylist并非线程安全,所以当我同时添加位图时,有时将不会添加和同步它们。因此,同步的方法之一就是使用同步块这样的方式。...

1-定义一个虚拟对象。 2-每次使用arraylist时,请在同步块中执行此操作。

1-

synchronized (mutex) {
                            myArrayList.add(myBitmapObject);
                        }

2-

class Category {
        static function create($id, $parentId, $name) {
                $category = new Category();
                $category->id = $id;
                $category->parentId = $parentId;
                $category->name = $name;
                return $category;
        }
        function add_sub_cat($cat) {
                $cat->parentId = $this;
        }
}