我按照本指南为 Galaxy Nexus 创建手电筒模组: 打开 Galaxy Nexus 的手电筒
当我启动它时,我有一个空指针异常,我不知道为什么?!
import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements SurfaceHolder.Callback{
Camera mCamera;
public static SurfaceView preview;
public static SurfaceHolder mHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("test", "1");
preview = (SurfaceView) findViewById(R.id.surfaceView);
Log.v("test", "2");
mHolder.addCallback(this);
Log.v("test", "3");
mHolder = preview.getHolder();
Log.v("test", "4");
mCamera = Camera.open();
Log.v("test", "5");
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button onLEDbtn = (Button) findViewById(R.id.onLED_btn);
onLEDbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mHolder = holder;
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview();
mHolder = null;
}
}
日志猫
11-06 17:13:02.675: V/test(17050): 1
11-06 17:13:02.675: V/test(17050): 2
11-06 17:13:02.679: D/AndroidRuntime(17050): Shutting down VM
11-06 17:13:02.679: W/dalvikvm(17050): threadid=1: thread exiting with uncaught exception (group=0x40d34300)
11-06 17:13:02.695: E/AndroidRuntime(17050): FATAL EXCEPTION: main
11-06 17:13:02.695: E/AndroidRuntime(17050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.torchnexus/com.example.torchnexus.MainActivity}: java.lang.NullPointerException
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.os.Looper.loop(Looper.java:137)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-06 17:13:02.695: E/AndroidRuntime(17050): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 17:13:02.695: E/AndroidRuntime(17050): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 17:13:02.695: E/AndroidRuntime(17050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-06 17:13:02.695: E/AndroidRuntime(17050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-06 17:13:02.695: E/AndroidRuntime(17050): at dalvik.system.NativeStart.main(Native Method)
11-06 17:13:02.695: E/AndroidRuntime(17050): Caused by: java.lang.NullPointerException
11-06 17:13:02.695: E/AndroidRuntime(17050): at com.example.torchnexus.MainActivity.onCreate(MainActivity.java:32)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.Activity.performCreate(Activity.java:5008)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-06 17:13:02.695: E/AndroidRuntime(17050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-06 17:13:02.695: E/AndroidRuntime(17050): ... 11 more