使用TextureView
我的应用相机预览。
现在的情况,
在我的应用中点击button
启动wifiapp
Intent intent = getPackageManager().getLaunchIntentForPackage("kr.co.aaa.wifiapp");
startActivity(intent);
目前,请致电onSurfaceTextureDestroyed
,onPause
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
并在wifiapp中,点击后退button
,重启我的应用。
此时,请致电onSurfaceTextureAvailable
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
try {
mCamera.setPreviewTexture(surfaceTexture); //NPE
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
重启我的应用时,会发生NullPointerException
java.lang.NullPointerException:尝试调用虚方法' void android.hardware.Camera.setPreviewTexture(android.graphics.SurfaceTexture)' 在null对象引用上 at 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) at 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)
我认为发生了NPE onSurfaceTextureDestroyed
mCamera = null;
。
因为当我的应用重启时,mCamera状态为空调用onSurfaceTextureAvailable
,mCamera.setPreviewTexture(surfaceTexture)
为空。
所以我尝试删除mCamera = null
但会出现不同的错误。
java.lang.RuntimeException:之后正在使用相机 调用了Camera.release() 在android.hardware.Camera.setPreviewTexture(本机方法) at 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) at 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)
此错误mCamera.setPrevieTexture(surfaceTexture)
如何解决这个问题?
感谢。
完整的来源
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 this line, occur Camera is being used after Camera.release() was 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;
}
如果需要更多信息。请评论。 感谢!
答案 0 :(得分:0)
请检查您的班级名称和构造名称
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
}
答案 1 :(得分:0)
将surfacetexture对象保存为全局变量,如果为null,则尝试从全局变量中获取保存的对象。