简体   繁体   中英

Null Pointer on an attempt to call a button

I have a null pointer on a button that I need to take me to a new layout when pushed. I have the code set as:

((Button) findViewById(R.id.analyzee)).setOnClickListener(btnClick);

inside a method that uses conditional statements.

It is a basic face detect app. If faces are not found, I do this:

if (facesFound < 1) {
                mFlipper.setDisplayedChild(2);
                mTheMessage = (TextView) findViewById(R.id.falsemessage);
                mThePicture = (ImageView) findViewById(R.id.false_view);
                mTheMessage.setText(R.string.noFaceOne);
                mThePicture.setImageBitmap(bitmap565);
                return; 

if faces are found, I draw a box on the face, and do this:

mFlipper.setDisplayedChild(1);
            mTheMessage.setText(R.string.noFaceFive);
            mThePicture.setImageBitmap(bitmap565);

My issue lies here though, I use this method to run when buttons are clocked:

private final View.OnClickListener btnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.scan_box:
                openCamera();
                break;

            case R.id.crop_face:
                final ProgressDialog dialog = ProgressDialog.show(Main.this, "",
                        "Cropping photo", true);
                dialog.show();

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                    }
                }, 3000);

                cropFace();
                break;

So, my issue lies in this:

In one of my layouts in the flipper, the button that rests on the layout will need to give the user the option to snap a new picture if there are no faces found. The other layout will need the button (upon click) to have the faces cropped and the results to be sent to another layout.

The issue I am facing is where the code:

((Button) findViewById(R.id.crop_face)).setOnClickListener(btnClick);

needs to be placed in order for the program to release the button has been clicked, it calls the case in my switch statement, and runs the crop face_method.

I try putting it in the if statement where I set the image View and text View, but I get a null pointer on that line I am declaring my button on.

The buttons I have on my main menu work fine, as they are in my onCreate method, but I don't know where to play this button command, and also where I need to place my reopen Camera command.

Thanks!

我只是使用了错误的按钮ID ...抱歉造成混乱= x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM