Galaxy Nexus上的LED手电筒:app传递了NULL表面

时间:2013-03-16 11:44:55

标签: java android camera surfaceview flashlight

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

LED flashlight on Galaxy Nexus controllable by what API?

Opening Flashlight of 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”?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

@Override

 public void surfaceCreated(SurfaceHolder holder) {

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

        e.printStackTrace();
    }
}
camera is
camera=Camera.open();