0

我什至正在尝试在相机键单击时从相机视图中捕获图像。它工作正常,但我想更改 UI 并使用按钮单击捕获,因此在 main.xml 程序中放置的按钮没有显示错误,但是当我运行程序时,它在 button.setOnClickListener(new View. OnClickListener()。我检查了我是否拼错了按钮 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()?提前谢谢

4

2 回答 2

0

您正在使用mPreview内容视图而不是布局。这就是为什么findViewById(R.id.button_click);返回 null 而您不处理此错误的原因。

于 2013-02-11T16:04:58.380 回答
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”中。

如果不将其添加为 .xml,则无法访问任何 xml 的视图控件contentview

于 2013-02-11T16:05:09.603 回答