我试图从相机视图中捕捉图像,甚至在相机键上点击。它工作正常,但我想更改UI并使用按钮单击捕获,因此在main.xml中放置按钮 程序显示没有错误,但是当我运行程序时,它会在行button_setOnClickListener(新的View.OnClickListener()时给出运行时错误和nullpointer异常。我检查了我是否拼错了按钮Id名称,但它是相同的。 `public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
//make the screen full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//remove the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
mResultView=new ResultView(this);
mPreview = new Preview(this);
//set Content View as the preview
setContentView(mPreview);
//add result view to the content View
addContentView(mResultView,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//set the orientation as landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Button capture_Button = (Button) findViewById(id.button_click);
button = (Button) findViewById(R.id.button_click);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCameraReadyFlag = false;
mPreview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
}`
这是我的主要活动代码
enter code here
02-11 19:21:58.367: I/Process(5729): Sending signal. PID: 5729 SIG: 9
02-11 19:22:04.987: D/AndroidRuntime(6006): Shutting down VM
02-11 19:22:04.987: W/dalvikvm(6006): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 19:22:04.987: E/AndroidRuntime(6006): FATAL EXCEPTION: main
02-11 19:22:04.987: E/AndroidRuntime(6006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ee368.siftexample/com.ee368.siftexample.SIFTExampleActivity}: java.lang.NullPointerException
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.os.Looper.loop(Looper.java:130)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 19:22:04.987: E/AndroidRuntime(6006): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 19:22:04.987: E/AndroidRuntime(6006): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 19:22:04.987: E/AndroidRuntime(6006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 19:22:04.987: E/AndroidRuntime(6006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 19:22:04.987: E/AndroidRuntime(6006): at dalvik.system.NativeStart.main(Native Method)
02-11 19:22:04.987: E/AndroidRuntime(6006): Caused by: java.lang.NullPointerException
02-11 19:22:04.987: E/AndroidRuntime(6006): at com.ee368.siftexample.SIFTExampleActivity.onCreate(SIFTExampleActivity.java:89)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 19:22:04.987: E/AndroidRuntime(6006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 19:22:04.987: E/AndroidRuntime(6006): ... 11 more
02-11 19:22:09.097: I/Process(6006): Sending signal. PID: 6006 SIG: 9'
这是我的日志窗口
这是xml布局
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture" />
</LinearLayout>
有人可以帮我解决这个错误吗?以及如何使用按钮而不是onkeydown()?提前谢谢
答案 0 :(得分:0)
您使用mPreview
作为内容视图而不是布局。
这就是findViewById(R.id.button_click);
返回null并且您不处理此错误的原因。
答案 1 :(得分:0)
您要将此布局添加到您的活动中:
mResultView=new ResultView(this);
mPreview = new Preview(this);
//set Content View as the preview
setContentView(mPreview);
//add result view to the content View
addContentView(mResultView,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
正如你所提到的,“把按钮放在main.xml
”中。
如果不将其添加为contentview
,则无法访问任何xml的视图控件。