我创建了一个应用程序,我有一个相机预览。我正在尝试创建一个触摸事件,这样当我触摸预览时,会发生一些事情(比方说,会出现一个toast)。 问题是当我触摸预览时,2秒后应用程序崩溃。 我有两个班:
CameraActivity:
public class CameraActivity extends Activity {
private static final String TAG = "CameraDemo";
Preview preview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
((FrameLayout) findViewById(R.id.preview)).setOnTouchListener(preview);
Log.d(TAG, "Camera Activity Created.");
}
}
和预览:
class Preview extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// Called once the holder is ready
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
// Called for each frame previewed
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
// Called when the holder is destroyed
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
// Called when holder has changed
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.startPreview();
}
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(findViewById(R.id.preview).getContext(), "BEFORE SHUTTER", Toast.LENGTH_LONG).show();
return false;
}
}
有人可以帮忙告诉我我做错了吗?
这是LogCat(出现崩溃的部分):
03-08 12:11:48.335: D/AndroidRuntime(1167): Shutting down VM
03-08 12:11:48.335: W/dalvikvm(1167): threadid=1: thread exiting with uncaught exception (group=0x40015578)
03-08 12:11:48.343: E/AndroidRuntime(1167): FATAL EXCEPTION: main
03-08 12:11:48.343: E/AndroidRuntime(1167): java.lang.NullPointerException
03-08 12:11:48.343: E/AndroidRuntime(1167): at com.marakana.Preview.onTouch(Preview.java:80)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.View.dispatchTouchEvent(View.java:3934)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
03-08 12:11:48.343: E/AndroidRuntime(1167): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1733)
03-08 12:11:48.343: E/AndroidRuntime(1167): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-08 12:11:48.343: E/AndroidRuntime(1167): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1717)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2215)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.os.Looper.loop(Looper.java:123)
03-08 12:11:48.343: E/AndroidRuntime(1167): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-08 12:11:48.343: E/AndroidRuntime(1167): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 12:11:48.343: E/AndroidRuntime(1167): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 12:11:48.343: E/AndroidRuntime(1167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-08 12:11:48.343: E/AndroidRuntime(1167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-08 12:11:48.343: E/AndroidRuntime(1167): at dalvik.system.NativeStart.main(Native Method)
03-08 12:11:48.378: D/dalvikvm(1167): GC_FOR_MALLOC freed 943K, 59% free 2633K/6407K, external 1596K/2108K, paused 15ms
03-08 12:11:48.531: D/dalvikvm(1167): GC_CONCURRENT freed 21K, 52% free 3084K/6407K, external 1596K/2108K, paused 2ms+26ms
03-08 12:11:56.109: I/Process(1167): Sending signal. PID: 1167 SIG: 9
由于
答案 0 :(得分:2)
您的上下文存在问题
Toast.makeText(findViewById(R.id.preview).getContext(), "BEFORE SHUTTER", Toast.LENGTH_LONG).show();
你无法以这种方式获得上下文。在构造函数中传递上下文:
Preview(Context context) {
我会创建一个私有的上下文实例
private Context ctx;
然后在构造函数中添加
this.ctx = context;
然后将toast消息更改为
Toast.makeText(ctx, "BEFORE SHUTTER", Toast.LENGTH_LONG).show();
这也将为您提供班级中大多数其他地方的背景,从而使生活更轻松。