0

我一直在尝试让我的 Galaxy Nexus 上的 LED 手电筒亮起,在 StackOverflow 上使用了几种不同的解决方案:

Galaxy Nexus 上的 LED 手电筒可由什么 API 控制?

Galaxy Nexus 打开手电筒

但我仍然收到此错误,我不明白为什么:

相机:应用程序通过 NULL 表面

这是我的代码(或者更确切地说是来自其他解决方案的代码稍作编辑):

package com.quinny898.doorcontrol;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class DoorControlDoing extends Activity implements Callback {
public Camera mCamera;

  /** Called when the activity is first created. */

  public void onCreate(Bundle savedInstanceState) {
      setContentView(R.layout.dialog_surface);
      SurfaceView preview = (SurfaceView) findViewById(R.id.surface_view);
      SurfaceHolder mHolder = preview.getHolder();
      mHolder.addCallback(this);


      super.onCreate(savedInstanceState);
      setContentView(R.layout.dialog_surface);
      Camera mCamera = Camera.open();
      Parameters params = mCamera.getParameters();
      params.setFlashMode(Parameters.FLASH_MODE_TORCH);
      mCamera.setParameters(params);      
      mCamera.startPreview();
      try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }




public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {}

public void surfaceCreated(SurfaceHolder holder) {
    setContentView(R.layout.dialog_surface);
    SurfaceView preview = (SurfaceView) findViewById(R.id.surface_view);
      SurfaceHolder mHolder = preview.getHolder();
    mHolder = holder;
    try {
        mCamera.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    setContentView(R.layout.dialog_surface);
    SurfaceView preview = (SurfaceView) findViewById(R.id.surface_view);
      SurfaceHolder mHolder = preview.getHolder();
    mCamera.stopPreview();
    mHolder = null;
}
}

这是布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <SurfaceView
    android:id="@+id/surface_view"
    android:layout_width="1px"
    android:layout_height="1px"/>
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/hi"/>
</LinearLayout>

我看不出这里有什么问题,所有的 ID 都是正确的,并且它会很好地膨胀布局,那么为什么它是“NULL”?

谢谢你的帮助!

4

1 回答 1

0
@Override

 public void surfaceCreated(SurfaceHolder holder) {

    try {
        camera.setPreviewDisplay(holder);
    } catch (Exception e) {

        e.printStackTrace();
    }
}
camera is
camera=Camera.open();
于 2013-04-28T13:01:36.603 回答