When using Camera TextureView , occur nullpointerExceptipn on android -
my app camera preview using textureview
.
now situation, when in app clicked button
start wifiapp
intent intent = getpackagemanager().getlaunchintentforpackage("kr.co.aaa.wifiapp"); startactivity(intent);
at time, call onsurfacetexturedestroyed
, onpause
@override public boolean onsurfacetexturedestroyed(surfacetexture surfacetexture) { mcamera.setpreviewcallback(null); mcamera.release(); mcamera = null; }
and in wifiapp, click button
, restart app.
at time, call onsurfacetextureavailable
@override public void onsurfacetextureavailable(surfacetexture surfacetexture, int width, int height) { try { mcamera.setpreviewtexture(surfacetexture); //npe mcamera.startpreview(); } catch (ioexception e) { e.printstacktrace(); } }
when restart app, occur nullpointerexception
java.lang.nullpointerexception: attempt invoke virtual method 'void android.hardware.camera.setpreviewtexture(android.graphics.surfacetexture)' on null object reference @ kr.co.iosystem.blackeyeonandroid.video.cameratextureview.onsurfacetextureavailable(cameratextureview.java:82) @ android.view.textureview.gethardwarelayer(textureview.java:368) @ android.view.view.updatedisplaylistifdirty(view.java:15157) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.draw(view.java:16187) @ android.view.view.updatedisplaylistifdirty(view.java:15180) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.updatedisplaylistifdirty(view.java:15175) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.draw(view.java:16187) @ android.view.view.updatedisplaylistifdirty(view.java:15180) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.updatedisplaylistifdirty(view.java:15175) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.updatedisplaylistifdirty(view.java:15175) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.draw(view.java:16187) @ com.android.internal.policy.phonewindow$decorview.draw(phonewindow.java:2690) @ android.view.view.updatedisplaylistifdirty(view.java:15180) @ android.view.threadedrenderer.updateviewtreedisplaylist(threadedrenderer.java:282) @ android.view.threadedrenderer.updaterootdisplaylist(threadedrenderer.java:288) @ android.view.threadedrenderer.draw(threadedrenderer.java:323) @ android.view.viewrootimpl.draw(viewrootimpl.java:2609) @ android.view.viewrootimpl.performdraw(viewrootimpl.java:2428) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:2061) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1107) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6007) @ android.view.choreographer$callbackrecord.run(choreographer.java:858) @ android.view.choreographer.docallbacks(choreographer.java:670) @ android.view.choreographer.doframe(choreographer.java:606) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:844) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5415) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:745) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:635)
i think occur npe onsurfacetexturedestroyed
mcamera = null;
.
because when app restart, mcamera status null call onsurfacetextureavailable
, mcamera.setpreviewtexture(surfacetexture)
null.
so try delete mcamera = null
but occur diffrent error.
java.lang.runtimeexception: camera being used after camera.release() called @ android.hardware.camera.setpreviewtexture(native method) @ kr.co.iosystem.blackeyeonandroid.video.cameratextureview.onsurfacetextureavailable(cameratextureview.java:82) @ android.view.textureview.gethardwarelayer(textureview.java:368) @ android.view.view.updatedisplaylistifdirty(view.java:15157) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.draw(view.java:16187) @ android.view.view.updatedisplaylistifdirty(view.java:15180) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.updatedisplaylistifdirty(view.java:15175) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.draw(view.java:16187) @ android.view.view.updatedisplaylistifdirty(view.java:15180) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.updatedisplaylistifdirty(view.java:15175) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.updatedisplaylistifdirty(view.java:15175) @ android.view.view.draw(view.java:15954) @ android.view.viewgroup.drawchild(viewgroup.java:3609) @ android.view.viewgroup.dispatchdraw(viewgroup.java:3399) @ android.view.view.draw(view.java:16187) @ com.android.internal.policy.phonewindow$decorview.draw(phonewindow.java:2690) @ android.view.view.updatedisplaylistifdirty(view.java:15180) @ android.view.threadedrenderer.updateviewtreedisplaylist(threadedrenderer.java:282) @ android.view.threadedrenderer.updaterootdisplaylist(threadedrenderer.java:288) @ android.view.threadedrenderer.draw(threadedrenderer.java:323) @ android.view.viewrootimpl.draw(viewrootimpl.java:2609) @ android.view.viewrootimpl.performdraw(viewrootimpl.java:2428) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:2061) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1107) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6007) @ android.view.choreographer$callbackrecord.run(choreographer.java:858) @ android.view.choreographer.docallbacks(choreographer.java:670) @ android.view.choreographer.doframe(choreographer.java:606) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:844) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5415) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:745) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:635)
this error mcamera.setprevietexture(surfacetexture)
how solve problem?
thanks.
full source
public class cameratexture extends textureview implement textureview.surfacetexturelistener { private camera mcamera; private textureview mtextureview; public cameratexture(context context, livecamera livecamera) { super(context); mcamera = livecamera.camera; mtextureveiw = (textureview) activity.findviewbyid(r.id.live); mtextureview.setsurfacetexturelistener(this); } @override public void onsurfacetextureavailable(surfacetexture surfacetexture, int width, int height) { try { mcamera.setpreviewtexture(surfacetexture); mcamera.startpreview(); } catch (ioexception e) { e.printstacktrace(); } } @override public void onsurfacetexturesizechanged(surfacetexture surfacetexture, int i, int i2) { } @override public boolean onsurfacetexturedestroyed(surfacetexture surfacetexture) { mcamera.setpreviewcallback(null); mcamera.release(); mcamera = null; // if delete line, occur camera being used after camera.release() called error } @override public void onsurfacetextureupdated(surfacetexture surfacetexture) { bitmap bitmap = mtextureview.getbitmap(); int width = bitmap.getwidth(); int height = bitmap.getheight(); int[] pixels = new int[bitmap.getheight() * bitmap.getwidth()]; bitmap.getpixels(pixels, 0, width, 0, 0, width, height); } } public class livecamera { public camera camera; }
if need more information. please comment. thanks.!
please check class name , constructor name
public class cameratexture extends textureview implement textureview.surfacetexturelistener { private camera mcamera; private textureview mtextureview; public cameratexture(context context, livecamera livecamera) { super(context); mcamera = livecamera.camera; mtextureveiw = (textureview) activity.findviewbyid(r.id.live); mtextureview.setsurfacetexturelistener(this); } //your remain code }
Comments
Post a Comment