Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams when In my app clicked button start wifiapp

Intent intent = getPackageManager().getLaunchIntentForPackage("kr.co.aaa.wifiapp");
startActivity(intent);

At this time, call onSurfaceTextureDestroyed , onPause

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
    mCamera.setPreviewCallback(null);
    mCamera.release();
    mCamera = null;

and In wifiapp, click back button, restart my app.

at this 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 my app, occur NullPointerException

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewTexture(android.graphics.SurfaceTexture)' on a null object reference at kr.co.iosystem.blackeyeonandroid.video.CameraTextureView.onSurfaceTextureAvailable(CameraTextureView.java:82) at android.view.TextureView.getHardwareLayer(TextureView.java:368) at android.view.View.updateDisplayListIfDirty(View.java:15157) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.draw(View.java:16187) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.updateDisplayListIfDirty(View.java:15175) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.draw(View.java:16187) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.updateDisplayListIfDirty(View.java:15175) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.updateDisplayListIfDirty(View.java:15175) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.draw(View.java:16187) at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:282) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:288) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:323) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2609) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2428) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2061) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6007) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5415) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

I think occur NPE onSurfaceTextureDestroyed mCamera = null; .

because when my app restart, mCamera status is null call onSurfaceTextureAvailable , mCamera.setPreviewTexture(surfaceTexture) is null.

so I try delete mCamera = null

but occur diffrent error.

java.lang.RuntimeException: Camera is being used after Camera.release() was called at android.hardware.Camera.setPreviewTexture(Native Method) at kr.co.iosystem.blackeyeonandroid.video.CameraTextureView.onSurfaceTextureAvailable(CameraTextureView.java:82) at android.view.TextureView.getHardwareLayer(TextureView.java:368) at android.view.View.updateDisplayListIfDirty(View.java:15157) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.draw(View.java:16187) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.updateDisplayListIfDirty(View.java:15175) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.draw(View.java:16187) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.updateDisplayListIfDirty(View.java:15175) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.updateDisplayListIfDirty(View.java:15175) at android.view.View.draw(View.java:15954) at android.view.ViewGroup.drawChild(ViewGroup.java:3609) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399) at android.view.View.draw(View.java:16187) at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690) at android.view.View.updateDisplayListIfDirty(View.java:15180) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:282) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:288) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:323) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2609) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2428) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2061) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6007) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5415) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

this error mCamera.setPrevieTexture(surfaceTexture)

How to solve this 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 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;

if need more information. please comment. thanks.!

Please check you class name and 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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.