I want to create a background with a camera on SurfaceView, and I will be able to use the Android API below 23. Although he previously requested camera resolution in the API 23 application, the following error appeared on my Nexus 5 with Android 6:
java.lang.NullPointerException: attempt to call the void virtual method android.hardware.Camera.setPreviewDisplay (android.view.SurfaceHolder) 'on a link with a null object on com.package.name.CameraPreview.surfaceCreated (CameraPreview.java:31)
My code is:
import android.content.Context; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera;;
The error goes to mCamera.setPreviewDisplay (holder);
My main activity code:
public class MainActivity extends AppCompatActivity { private static Context mContext; private Camera mCamera; private CameraPreview mPreview; private FrameLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); layout = (FrameLayout) findViewById(R.id.camera_preview); mCamera = getCameraInstance(); mPreview = new CameraPreview(this, mCamera); layout.addView(mPreview); } public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open();
My manifest received:
<uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" />
Any ideas to solve this problem on Android 6?
Thanks for your time, greetings.
java android android-studio camera
mmsergi
source share