Note that there are some explanatory texts on larger screens.

plurals
  1. PO.setPreviewDisplay(holder) creating NullPointer exception
    primarykey
    data
    text
    <p>I have created a custom camera application and it works perfectly fine on almost all devices but it seems to be crashing on devices listed as <strong>other</strong> in the developer portal. These users are all giving me bad reviews I would like to solve this problem but it is very hard for me to figure it out, because it works perfectly on all the devices I own.</p> <p><code>camera.setPreviewDisplay(holder);</code> is what is causing the error and it is occurring when the app launches.`</p> <pre><code>public class CamPreview extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder holder; Camera camera; private List&lt;Camera.Size&gt; sizes; public CamPreview(Context context, Camera camera) { super(context); this.camera = camera; holder = this.getHolder(); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.addCallback(this); } private Camera.Size getBestPreviewSize(int width, int height) { Camera.Size result=null; Camera.Parameters p = camera.getParameters(); for (Camera.Size size : p.getSupportedPreviewSizes()) { if (size.width&lt;=width &amp;&amp; size.height&lt;=height) { if (result==null) { result=size; } else { int resultArea=result.width*result.height; int newArea=size.width*size.height; if (newArea&gt;resultArea) { result=size; } } } } return result; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (holder.getSurface() == null) return; Camera.Parameters parameters = camera.getParameters(); sizes = parameters.getSupportedPreviewSizes(); Camera.Size optimalSize = getBestPreviewSize(width, height); try{ parameters.setPreviewSize(optimalSize.width,optimalSize.height); camera.setParameters(parameters); } catch (NullPointerException a) { } camera.startPreview(); } @Override public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); } catch (IOException e) { camera.release(); camera=null; Log.d("FrontCam", "Error creating surface: " + e.getMessage()); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera != null) { camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); } } } </code></pre> <p>logcat:</p> <pre><code> java.lang.NullPointerException at com.me.frontcam.CamPreview.surfaceCreated(CamPreview.java:136) at android.view.SurfaceView.updateWindow(SurfaceView.java:552) at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.View.draw(View.java:6883) at android.widget.FrameLayout.draw(FrameLayout.java:357) at android.view.ViewGroup.drawChild(ViewGroup.java:1646) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.View.draw(View.java:6883) at android.widget.FrameLayout.draw(FrameLayout.java:357) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1921) at android.view.ViewRoot.draw(ViewRoot.java:1528) at android.view.ViewRoot.performTraversals(ViewRoot.java:1264) at android.view.ViewRoot.handleMessage(ViewRoot.java:1866) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>how I open the camera</p> <pre><code>void initCamera() { //check for front camera if(Camera.getNumberOfCameras()&gt;1) { camera= Camera.open(1); try { camera.cancelAutoFocus(); } catch (RuntimeException a) { } try{ camera.setDisplayOrientation(90); } catch (NullPointerException a) { camera.setDisplayOrientation(90); } } else { new AlertDialog.Builder(this) .setTitle("Sorry but you cant use this app") .setMessage("You do not have a front camera") .setPositiveButton("Quit", new DialogInterface.OnClickListener() { //Quits application public void onClick(DialogInterface dialog, int which) { finish(); } }) .show(); } } </code></pre> <p>I then call the initUI method where the CamPreview constructor is called</p> <pre><code>void initUI() { white= new ImageView(this); imgShoot = (ImageButton) findViewById(R.id.imgShoot); imgShoot.setOnClickListener(imgShootOnClick); frmPreview = (FrameLayout) findViewById(R.id.frmPreview); preview = new CamPreview(this, camera); frmPreview.addView(white); white.setImageResource(R.drawable.aperture_opening); frmPreview.addView(preview); frmPreview.bringChildToFront(preview); } </code></pre> <p>Ive looked at both of these and they did not solve my problem <a href="https://stackoverflow.com/questions/6825129/android-cam-setpreviewdisplayholder-running-into-ioerror">Android - cam.setPreviewDisplay(holder) running into IOError</a></p> <p><a href="https://stackoverflow.com/questions/8984542/setpreviewdisplayholder-throwing-null-pointer-exception?lq=1">.setPreviewDisplay(holder) throwing null pointer exception</a></p> <p>Any help appreciated greatly!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload