I'm getting this exception raised from SurfaceView.onAttachedToWindow. It looks like SurfaceView is trying to reference mParent, but it's null. Does anyone know why the parent wouldn't be set but onAttachedToWindow would be called?
I'm using the SurfaceView for a camera preview. I have a ViewGroup that is added/removed from the activity's root view. Inside the ViewGroup is the SurfaceView and a WebView.
Here is the code for the ViewGroup: http://pastebin.com/eSyZ3v3P and here is the code that show/hides the camera: http://pastebin.com/5iaEFCQr
Any help would be really appreciated.
Here is the full stack trace:
java.lang.NullPointerException
at android.view.SurfaceView.onAttachedToWindow(SurfaceView.java:207)
at android.view.View.dispatchAttachedToWindow(View.java:11755)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1199)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
-- edit --
Sorry, I forgot to mention, I'm not aware of the steps to reproduce the exception. The code works every time I run the app, but I'm getting these exceptions sent to my logger from a Samsung Galaxy Tab 3. And the issue isn't consistent.
I believe your bug is because you are using a stale View in this line:
setPreviewDisplay(this.surfaceView.getHolder())
In this line, when it crashes, surfaceView is no longer valid. You should use the SurfaceHolder returned in surfaceChanged/surfaceCreated.
This was the source of my bug also.
In general:
This error can occur when you are trying to attach a view (your preview) to a window that no longer exists. For example, you have some long running code (opening the camera) and then you attach the view, and the phone has rotated (old context) or another app is showing (so its not valid to attach to the unseen view), or the lock screen is on, etc.
One way to avoid this situation is to make sure you have a visible window for your Activity before you try to attach your view. I wish I had more details to tell you how to do that.
One possible issue is that you are doing Camera.open(), which can take some time, especially on certain devices, before you call addView(), so maybe by the time you get around to addView(), the window is not available. Consider doing Camera.open() on a background thread, and when all that is done, do addView().
I see that you have several threads running on the UI thread. You want to make sure those are all done before you do the addView.
I don't know what causes it but I could think of only 1 work around:
this.surfaceView = new SurfaceView(customContext) {
/* (non-Javadoc)
* @see android.view.SurfaceView#onAttachedToWindow()
*/
@Override
protected void onAttachedToWindow() {
if(this.getParent() != null)
super.onAttachedToWindow();
}
};
Can't test it though to see if it will create more issues or just fix this one.
I know it's hackish but I thought it wouldn't hurt to write it.
Try it at your own risk!
Inside the constructor of the class CameraPluginView , you pass customContext
this.surfaceView = new SurfaceView(getContext());
Instead of the previous call, try this constructor :
SurfaceView(Context context, AttributeSet attrs)
which is "called when inflating a view from XML"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With