android – surfaceView.getHolder没有返回SurfaceHolder

概述我正在尝试编写使用相机的应用程序.在尝试获取我最终传递给启动摄像头的surfaceCreated()的surfaceHolder时,我得到一个NullPointerException. getHolder()返回NULL时有什么情况吗? 谢谢. package com.tecmark;import java.io.IOException;import android.app.Activit 我正在尝试编写使用相机的应用程序.在尝试获取我最终传递给启动摄像头的surfaceCreated()的surfaceHolder时,我得到一个NullPointerException. getHolder()返回NulL时有什么情况吗?

谢谢.

package com.tecmark;import java.io.IOException;import androID.app.Activity;import androID.graphics.PixelFormat;import androID.harDWare.Camera;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.vIEw.Window;import androID.vIEw.WindowManager;public class cameraview extends Activity implements SurfaceHolder.Callback{    SurfaceVIEw mSurfaceVIEw;    SurfaceHolder mSurfaceHolder;    Camera mCamera;    boolean mPrevIEwRunning; /** Called when the activity is first created. */    @OverrIDe     public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getwindow().setFormat(PixelFormat.TRANSLUCENT);        requestwindowFeature(Window.FEATURE_NO_Title);        getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN,WindowManager.LayoutParams.FLAG_FulLSCREEN);        //setContentVIEw(R.layout.camera_surface);           mSurfaceVIEw = (SurfaceVIEw) findVIEwByID(R.ID.surface_camera);        Log.i("surfaceholder = ","about to get surface holder");        try{        mSurfaceHolder = mSurfaceVIEw.getHolder();        }catch(Exception e){         e.printstacktrace();        }        Log.i("surfaceholder = ",""+mSurfaceHolder.toString());        mSurfaceHolder.addCallback(this);        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        setContentVIEw(R.layout.camera_surface);        surfaceCreated(mSurfaceHolder);    } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int w,int h) {  if (mPrevIEwRunning) {   mCamera.stopPrevIEw();   }   Camera.Parameters p = mCamera.getParameters();   p.setPrevIEwSize(w,h);   mCamera.setParameters(p);   try {   mCamera.setPrevIEwdisplay(holder);   } catch (Exception e) {   e.printstacktrace();   }   mCamera.startPrevIEw();   mPrevIEwRunning = true;   } @OverrIDe public voID surfaceCreated(SurfaceHolder holder) {  try{   Log.i("camera ","about to open camera");         mCamera = Camera.open();         Log.i("camera "," camera opened");         mCamera.getParameters();         mCamera.setPrevIEwdisplay(holder);         mCamera.startPrevIEw();   } catch (IOException e) {    e.printstacktrace();   }  Log.i("camera ","ok"); } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) {  mCamera.stopPrevIEw();  mPrevIEwRunning = false;  mCamera.release(); }}//end of activity01-31 15:29:17.773: WARN/System.err(9144): java.lang.NullPointerException01-31 15:29:17.778: WARN/System.err(9144):     at com.tecmark.cameraview.onCreate(cameraview.java:42)01-31 15:29:17.778: WARN/System.err(9144):     at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)01-31 15:29:17.778: WARN/System.err(9144):     at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2459)01-31 15:29:17.783: WARN/System.err(9144):     at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)01-31 15:29:17.783: WARN/System.err(9144):     at androID.app.ActivityThread.access00(ActivityThread.java:119)01-31 15:29:17.783: WARN/System.err(9144):     at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)01-31 15:29:17.783: WARN/System.err(9144):     at androID.os.Handler.dispatchMessage(Handler.java:99)01-31 15:29:17.783: WARN/System.err(9144):     at androID.os.Looper.loop(Looper.java:123)01-31 15:29:17.783: WARN/System.err(9144):     at androID.app.ActivityThread.main(ActivityThread.java:4363)01-31 15:29:17.783: WARN/System.err(9144):     at java.lang.reflect.Method.invokeNative(Native Method)01-31 15:29:17.788: WARN/System.err(9144):     at java.lang.reflect.Method.invoke(Method.java:521)01-31 15:29:17.788: WARN/System.err(9144):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)01-31 15:29:17.788: WARN/System.err(9144):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:618)01-31 15:29:17.788: WARN/System.err(9144):     at dalvik.system.NativeStart.main(Native Method)01-31 15:29:17.793: DEBUG/AndroIDRuntime(9144): Shutting down VM01-31 15:29:17.793: WARN/dalvikvm(9144): threadID=3: thread exiting with uncaught exception (group=0x4001b180)01-31 15:29:17.793: ERROR/AndroIDRuntime(9144): Uncaught handler: thread main exiting due to uncaught exception01-31 15:29:17.803: ERROR/AndroIDRuntime(9144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecmark/com.tecmark.cameraview}: java.lang.NullPointerException01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2496)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.ActivityThread.access00(ActivityThread.java:119)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.os.Handler.dispatchMessage(Handler.java:99)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.os.Looper.loop(Looper.java:123)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.ActivityThread.main(ActivityThread.java:4363)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at java.lang.reflect.Method.invokeNative(Native Method)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at java.lang.reflect.Method.invoke(Method.java:521)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:618)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at dalvik.system.NativeStart.main(Native Method)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144): Caused by: java.lang.NullPointerException01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at com.tecmark.cameraview.onCreate(cameraview.java:47)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2459)01-31 15:29:17.803: ERROR/AndroIDRuntime(9144):     ... 11 more
解决方法 开始一个新的答案,希望更清楚.同样,我认为根本问题是你在onCreate和onResume上做的事情的数量.

我认为您遇到的根本问题是视图/活动生命周期的某些部分需要在开始处理surfaceVIEw并开始使用它之前运行它们的过程.

我不确定这一点,但是这里有各种各样的地方的故障,我在一个增强现实应用程序中处理各种相机和表面视图的东西:

onCreate(){    // just set content vIEw. do nothing with the camera or surfaceVIEw yet    setContentVIEw(R.layout.main);}onResume(){        // open camera    mCamera = Camera.open();    // init surface vIEw    sv = (SurfaceVIEw)this.findVIEwByID(R.ID.SurfaceVIEw01);            mHolder = sv.getHolder();             mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);             mHolder.setSizefromLayout();            mHolder.addCallback(this); }surfaceChanged(SurfaceHolder holder,int h){    mCamera.setPrevIEwdisplay(holder);    // set any cam params you need...    mCamera.startPrevIEw();}

尝试重构代码以执行类似 *** 作.我的眼睛变得闷闷不乐试图从你的来源算出NPE的发生地点,但我认为有一些问题是:

>你似乎试图在调用setContentVIEw()之前尝试引用surfacevIEw
>你直接调用surfaceCreated方法,而不是让它通过回调调用.

希望有所帮助.我们还能解决这个问题!

总结

以上是内存溢出为你收集整理的android – surfaceView.getHolder没有返回SurfaceHolder全部内容,希望文章能够帮你解决android – surfaceView.getHolder没有返回SurfaceHolder所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存