[英]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. 需要放置以便程序被释放的按钮被单击,它在我的switch语句中调用case,并运行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. 我尝试将其放在设置图像视图和文本视图的if语句中,但是在该行上声明了一个空指针。
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. 我在主菜单上具有的按钮可以正常工作,就像在onCreate方法中一样,但是我不知道在哪里播放此按钮命令,以及在哪里放置重新打开Camera命令。
Thanks! 谢谢!
我只是使用了错误的按钮ID ...抱歉造成混乱= x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.