当我恢复活动时,预览不起作用,录制功能也不起作用.通过查看Logcat,我发现我的Camera对象等于null,我找不到它来重新创建它.
我的创造
mCamera = getCameraInstance(); // Create prevIEw vIEw and set it as the content of our activity. mPrevIEw = new CameraPrevIEw(this,mCamera); prevIEw = (FrameLayout) findVIEwByID(R.ID.prevIEwer); prevIEw.addVIEw(mPrevIEw);
获取相机实例方法
private Camera getCameraInstance() { Camera c = null; try { //c = Camera.open(); // attempt to get a Camera instance c = this.open(); // attempt to get a Camera instance } catch (Exception e) { // Camera is not available (in use or does not exist) } Log.i("TEST getCameraInstance","" + c); return c; // returns null if camera is unavailable }
我的暂停
protected voID onPause() { super.onPause(); try { // release the camera immediately on pause event // releaseCamera(); mCamera.stopPrevIEw(); mCamera.setPrevIEwCallback(null); mCamera.release(); mCamera = null; } catch (Exception e) { e.printstacktrace(); } }
我的简历
protected voID onResume() { super.onResume(); try { mCamera = Camera.open(); mCamera = getCameraInstance(); Log.i("TEST onresume","onResume Runing"); Log.i("TEST onresume","camera " + mCamera); mCamera.setPrevIEwCallback(null); Log.i("TEST onresume","camera " + mCamera); //mCamera = getCameraInstance(); Log.i("TEST onresume","camera " + mCamera); // mCamera.setPrevIEwCallback(null); mPrevIEw = new CameraPrevIEw(MainRecordScreen.this,mCamera);// set prevIEw Log.i("TEST onresume","camera " + mCamera); prevIEw.addVIEw(mPrevIEw); Log.i("TEST onresume","camera " + mCamera); mCamera.startPrevIEw(); } catch (Exception e) { Log.d(TAG,"Error starting camera prevIEw: " + e.getMessage()); } Log.i("TEST onresume","camera " + mCamera); }
我的LogCat
06-19 06:38:40.223: E/AndroIDRuntime(16702): FATAL EXCEPTION: main06-19 06:38:40.223: E/AndroIDRuntime(16702): java.lang.NullPointerException06-19 06:38:40.223: E/AndroIDRuntime(16702): at com.plcd.test.MainRecordScreen.prepareVIDeoRecorder(MainRecordScreen.java:198)06-19 06:38:40.223: E/AndroIDRuntime(16702): at com.plcd.test.MainRecordScreen.access(MainRecordScreen.java:193)06-19 06:38:40.223: E/AndroIDRuntime(16702): at com.plcd.test.MainRecordScreen.onClick(MainRecordScreen.java:115)06-19 06:38:40.223: E/AndroIDRuntime(16702): at androID.vIEw.VIEw.performClick(VIEw.java:3511)06-19 06:38:40.223: E/AndroIDRuntime(16702): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:14105)06-19 06:38:40.223: E/AndroIDRuntime(16702): at androID.os.Handler.handleCallback(Handler.java:605)06-19 06:38:40.223: E/AndroIDRuntime(16702): at androID.os.Handler.dispatchMessage(Handler.java:92)06-19 06:38:40.223: E/AndroIDRuntime(16702): at androID.os.Looper.loop(Looper.java:137)06-19 06:38:40.223: E/AndroIDRuntime(16702): at androID.app.ActivityThread.main(ActivityThread.java:4424)06-19 06:38:40.223: E/AndroIDRuntime(16702): at java.lang.reflect.Method.invokeNative(Native Method)06-19 06:38:40.223: E/AndroIDRuntime(16702): at java.lang.reflect.Method.invoke(Method.java:511)06-19 06:38:40.223: E/AndroIDRuntime(16702): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)06-19 06:38:40.223: E/AndroIDRuntime(16702): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)06-19 06:38:40.223: E/AndroIDRuntime(16702): at dalvik.system.NativeStart.main(Native Method)
如果有帮助,mCamera会从getCameraInstance()方法返回null.
解决方法 在你的onResume()中,你正在调用:mCamera = Camera.open();mCamera = getCameraInstance();
在这里发生了什么,你是通过Camera.open()得到一个相机实例,在这个语句之后,你再次尝试通过调用getCameraInstance()来获取相机实例;将返回null因为你的活动已经获得了实例,因此mCamera实例变为null.
如果这不能解决您的问题,请告诉我,我会看到其他一些解决方案.
总结以上是内存溢出为你收集整理的android – 活动恢复时相机预览屏幕黑屏全部内容,希望文章能够帮你解决android – 活动恢复时相机预览屏幕黑屏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)