- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个使用相机的应用程序。当我试图获取我最终传递给启动相机的 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 activity
01-31 15:29:17.773: WARN/System.err(9144): java.lang.NullPointerException
01-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.access$2200(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 VM
01-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 exception
01-31 15:29:17.803: ERROR/AndroidRuntime(9144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecmark/com.tecmark.cameraView}: java.lang.NullPointerException
01-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.access$2200(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.NullPointerException
01-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 并开始预览之前, View /Activity 生命周期的某些部分需要运行它们的过程。
我不确定,但这是我在一个有效的增强现实应用程序中处理各种相机和表面 View 内容的各个地方的分割:
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 format, int w, int h)
{
mCamera.setPreviewDisplay(holder);
// set any cam params you need...
mCamera.startPreview();
}
尝试重构您的代码以执行类似操作。我试图从您的来源中找出 NPE 发生的位置,但我认为有几个问题是:
希望对您有所帮助。我们会弄清楚的!
关于android - surfaceView.getHolder 不返回 SurfaceHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4852740/
本文整理了Java中am.widget.zxingscanview.ZxingScanView.getHolder()方法的一些代码示例,展示了ZxingScanView.getHolder()的具体
我正在尝试编写一个使用相机的应用程序。当我试图获取我最终传递给启动相机的 surfaceCreated() 的 surfaceHolder 时,我得到了一个 NullPointerException。
本文整理了Java中org.bouncycastle.cert.X509AttributeCertificateHolder.getHolder()方法的一些代码示例,展示了X509Attribute
我一直在尝试按照此处的教程进行操作: http://www.edu4java.com/en/androidgame/androidgame2.html 但是遇到了障碍。出于某种原因,eclipse 无
public class CameraFragment extends Fragment implements SurfaceHolder.Callback,View.OnClickListener
在我的 Activity 中,我有一些 View 和一个 surfaceView。 这是我在 onCreate() 中的第一个代码 public void onCreate(Bundle savedI
我将使用 Camera 简单地生成录像机应用程序。但它崩溃了,给我一个吼叫错误: Attempt to invoke virtual method 'android.view.SurfaceHolde
我是一名优秀的程序员,十分优秀!