Null Pointer on an attempt to call a button - android

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!

I simply was using the wrong button ID...sorry for the confusion =x

Related

Calling .getBackground() in universal onClick method

I want to include many buttons in my app, which can play a sound by clicking, so I included an OnClick event.
#Override
public void onClick(View view) {
int id = view.getId();
final MediaPlayer mediaPlayer = new MediaPlayer();
switch (id) {
case R.id.whisteling_bird:
stopandPlay(R.raw.whisteling_bird, mediaPlayer);
break;
default:
break;
}
}
But now I have the following problem:
I also want to change the Alpha value of the button by using
.getBackground().setAlpha(64);
But what do I have to write before .getBackground()?
I donĀ“t want to write this
final Button whisteling_bird = (Button) view.findViewById(R.id.whisteling_bird);
whisteling_bird.setOnClickListener(this);
whisteling_bird.getBackground().setAlpha(64);
for every single button. What can I do?
In your onClick(), below id line,
put view.getBackground().setAlpha(64);
it will set every clicked view's alpha to 64. But you will also need to reset it somewhere for safety.

Multiple Edittext with Buttons onClickListener

Currently I am working on an app that has somewhat of a basic calculator layout and functions. I have 8 edittext and a grid of 9 buttons. With the onClicklistener method is there a better way of handling this instead of nesting a long if or switch statement for each Edittext?
private View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId() /*to get clicked view id**/) {
case R.id.btn_1:
if edt_1.hasFocus() {
edt_1 = btn_1.getText()
}
if edt_2.hasFocus() {
edt_2 = btn_1.getText()
}
break;
case R.id.btn_2:
if edt_1.hasFocus() {
edt_1 = btn_1.getText()
}
if edt_2.hasFocus() {
edt_2 = btn_1.getText()
}
break;
default:
break;
}
}
I was thinking maybe it would be better to use a hasFocus() method and listen for clicks from there or a while loop, then maybe pass a variable?
Thanks in advance..
Hear is a way where you can small your code. just add android:onClick="onClick" in every button of your grid layout and do following in java file,
public void onClick(View v) {
Button b=(Button)v;
String s+=b.getText();
}
This two line get text from your button and set to the String s.
if you want to create calculator that function real-time calculation like this calculator
APK:https://play.google.com/store/apps/details?id=com.androchunk.calculator
then you can find free source code of calculator project as well as tutorial video that help you to create calculator and understand working of calculator.
Tutorial videos:https://www.youtube.com/playlist?list=PLdMmtAIsH0KYiKrdpbzat6t96Nb1_k3_1

Android Development - Create a menu with 3 imageButtons

First of all english is not my first language but i will try my best.
Also... i am pretty sure my title choice was not the best so sorry for that.
Basically what i wanted to do is a menu with three ImageButtons but there is a tricky part (tricky for me at least) since every time i press one button that same button changes image (to a colored version instead of a grayed out image) and the other two change as well from colored version of their respective images to grayed out ones, actually only one of the other two will change since the purpose of this is to be able to activate only one at a time so it would not be possible to have the other two active at the same time.
Notice that this is not a menu on the top right corner but just a set of three ImageButtons on a activity or Fragment.
I already tried a lot of stuff to make that happen but so far no luck but i think i know why though i can't find a workaround for this since i am actually new in android dev.
what i tried was inside the setOnClickListener of any of those buttons such as:
eventsButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
ImageButton eventsButton = (ImageButton) view.findViewById(R.id.eventsButton);
eventsButton.setBackgroundResource(R.drawable.events_icon_active);
eventsButton.setClickable(false);
}
}
);
i tried to add the functions to change the other imageButtons as well like:
eventsButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
ImageButton eventsButton = (ImageButton) view.findViewById(R.id.eventsButton);
eventsButton.setBackgroundResource(R.drawable.events_icon_inactive);
eventsButton.setClickable(false);
ImageButton contactsButton = (ImageButton) view.findViewById(R.id.contactsButton);
contactsButton.setBackgroundResource(R.drawable.contacts_icon_inactive);
contactsButton.setClickable(true);
ImageButton interestsButton = (ImageButton) view.findViewById(R.id.interestsButton);
interestsButton.setBackgroundResource(R.drawable.interests_icon_inactive);
interestsButton.setClickable(true);
}
}
);
and i repeated that three time, always setting the other buttons clickable and setting their images to the inactive one (the grayed out one), also setting the button i click as no longer clickable.
But from what i gather i cant do any references to any other buttons inside the eventsButton.setOnClickListener like the buttons interestsButton or contactsButton, it will crash the app as soon as i touch any of those three buttons with the following error message:
Attempt to invoke virtual method 'void android.widget.ImageButton.setBackgroundResource(int)' on a null object reference
And it always point to the first line where i make a reference to another button other then the one used to start the setOnClickListener.
If you can just point me in the right direction i would be tremendously grateful.
All the best
You can declare your ImageViews as final outside the scope of the listener and when the onClickListener(View v) is called you can then just call setBackground because they are final and you can reference them from inside the listener.
Something like this:
final ImageView view1 = (ImageView) findViewById(R.id.view1id);
final ImageView view2 = (ImageView) findViewById(R.id.view2id);
view1.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
// do whatever you want to the ImageViews
// view1.setBackground...
}
}
);
eventsButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
ImageButton contactsButton = (ImageButton) view.findViewById(R.id.contactsButton);
contactsButton.setBackgroundResource(R.drawable.contacts_icon_inactive);
contactsButton.setClickable(true);
}
}
);
Your problem is in view.findViewById(R.id.contactsButton): view here is the button being clicked (the events one), and by calling view.findViewById(contactsButton) you are implicitly saying that the contact button is a child of view, which is not.
Just use findViewById() (from Activity), getActivity().findViewById() (from Fragments), or better container.findViewById() (if you have a reference to the layout containing the three buttons).
I'm not saying that yours is the most efficient way to deal with a menu, just pointing out your error.
You can first make things simple; I suggest:
you add 3 array (Arraylist might be better) fields in your activity class, one for the buttons, one for the active resources and one for the inactive resources
initialize those arrays in the onCreate method;
define a single onClickListener object and use it for all the buttons; Use a loop in the onClick method, see bellow.
In terms of code, it looks like this:
ImageButton[] buttons;
int[] activeResources;
int[] inactiveResources;
protected void onCreate2(Bundle savedInstanceState) {
View.OnClickListener onClickListener = new View.OnClickListener(){
public void onClick(View view) {
ImageButton clickedButton = (ImageButton) view;
for(int i = 0; i<buttons.length; i++){
ImageButton bt = buttons[i];
if(clickedButton==bt){
bt.setBackgroundResource(inactiveResources[i]);
bt.setClickable(false);
}else{
bt.setBackgroundResource(activeResources[i]);
bt.setClickable(true);
}
}
}
};
buttons = new ImageButton[3];
activeResources = new int[3];
inactiveResources = new int[3];
int idx = 0;
buttons[idx] = (ImageButton) findViewById(R.id.eventsButton);
inactiveResources[idx] = R.drawable.events_icon_inactive;
activeResources[idx] = R.drawable.events_icon_active;
idx = 1;
buttons[idx] = (ImageButton) findViewById(R.id.contactsButton);
inactiveResources[idx] = R.drawable.contacts_icon_inactive;
activeResources[idx] = R.drawable.contacts_icon_active;
idx = 3;
buttons[idx] = (ImageButton) findViewById(R.id.interestsButton);
inactiveResources[idx] = R.drawable.interests_icon_inactive;
activeResources[idx] = R.drawable.interests_icon_active;
for(int i =0; i<buttons.length; i++){
buttons[i].setBackgroundResource(activeResources[i]);
buttons[i].setOnClickListener(onClickListener);
}
}
Do not expect it to run right the way, I am giving only ideas, you have to look and see if it fit for you are looking for.

Some alternative to hundreds of buttons

Im going to write some android app, which will basically consists of two activities. So first should have a lot of buttons (100+) and on click on any of them I will just get some special id and move to second activity. But is there any alternative to declare that hundreds of buttons and copy/paste one piece of code to every of them setting almost same onClickLister? Is there any special construction? Thanks
Edit: every of buttons are actually indexed from 1 to n. And on the click second activity will be launched and get that index to show it. I cant basically use any spinner or smth else, because there will be 3 rows of clickable things and each of them carring different images
Edit 2: so, to give you an idea, im going to do some table of buttons like in Angry Birds menu when you actually choosing the level you want to play. So, on click you will get id of button and start second activity
Call the method to add buttons
private void addButton(){
LinearLayout view = (LinearLayout) findViewById(R.id.linear_layout_id_here);
Button btn = null;
int w = 50;
int h = 25;
for(int i=1; i<100; i++) {
btn = new Button(this);
btn.setLayoutParams(new LayoutParams(w,h));
btn.setText("button " +i);
btn.setTag(""+i);
btn.setOnClickListener(onClickBtn);
view.addView(btn);
btn = null;
}
}
Call this method for handling onclick on button
private View.OnClickListener onClickBtn = new View.OnClickListener() {
public void onClick(View view) {
final int tag = Integer.parseInt(view.getTag().toString());
switch (tag) {
case 1:
// Do stuff
break;
case 2:
// Do stuff
break;
default:
break;
}
}
};
You should use a ListView.
ListViews are great for handling a lot of items at the same time. They are also natural for the user. Additionally, you use only one click listener - OnItemClickListener.
There's a useful example on how to work with ListViews in the Android Referenence.
You may add buttons in code, something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
/*your code here*/
GroupView gw =findViewById(R.id.pnlButtonscontainer); //find the panel to add the buttons
for(int i=0; i<100; i++) {
Button b = new Button(this);
b.setLayoutParameters(new LayoutParameters(w,h));
b.settext = i+"";
b.setOnClickListener(new OnClickListener(){
});
}
}
I coded directly into browser, so some syntax error may appear in my code, but this is the point, a way, not the only one, to add 100 buttons.

Android repeatable dialog buttons

I'm building a dialog that lets you click the picture multiple times, and each time you click it it changes the picture.
final Dialog dialog = new Dialog(ViewCase.this);
dialog.setContentView(R.layout.viewcase_largeimage);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle(name);
// show enlarged image
currPic = 1;
final ImageView imageViewLarge1 = (ImageView) dialog
.findViewById(R.id.imageViewViewCasePhotoLarge1);
imageViewLarge1.setImageBitmap(photoBitmap1);
imageViewLarge1
.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View view) {
switch (currPic) {
case 0:
imageViewLarge1
.setImageBitmap(photoBitmap1);
currPic++;
case 1:
imageViewLarge1
.setImageBitmap(photoBitmap2);
currPic++;
case 2:
imageViewLarge1
.setImageBitmap(photoBitmap3);
currPic = 0;
}
}
});
// shows the dialog
dialog.show();
}
This is my on click listener, and I can have allow for one click that changes to the second picture, but it stops after that. Any way to make the button click repeatable?
In switch block you should always use break; after every case. Switch doesn't stop executing when it finds the right case, it goes forward and executes every case. Maybe this can be the problem, you need to try it.

Categories

Resources