[help] i keep getting an error when i add an on click listener

Search This thread

beefmaster

New member
Jun 16, 2014
3
0
hey i have been learing java for a while and just have started coding for android last night

i am learing android development off the new boston youtube page

i have created some buttons in xml and assigned them ids and now would like to add an on click listener.

this is my code

Code:
package com.example.time;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class StartingPoint extends ActionBarActivity {

	Button yes,no;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_starting_point);
		
		yes = (Button) findViewById(R.id.bno);
		no = (Button) findViewById(R.id.byes);
		add.setonClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});

eclipse created an onclicklistner but has an error with add

the tutorial i am learning of of is from the late 2000s is there a new way or something have i done something wrong please help

this is my first post yaaah

sorry if this isnt allowed in this section of the forum i see that people are asking question here aswell but when i go to post this threads it says that you cant post it here

any way thxs for ur help in advance
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
hey i have been learing java for a while and just have started coding for android last night

i am learing android development off the new boston youtube page

i have created some buttons in xml and assigned them ids and now would like to add an on click listener.

this is my code

Code:
package com.example.time;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class StartingPoint extends ActionBarActivity {

	Button yes,no;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_starting_point);
		
		yes = (Button) findViewById(R.id.bno);
		no = (Button) findViewById(R.id.byes);
		add.setonClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});

eclipse created an onclicklistner but has an error with add

the tutorial i am learning of of is from the late 2000s is there a new way or something have i done something wrong please help

this is my first post yaaah

sorry if this isnt allowed in this section of the forum i see that people are asking question here aswell but when i go to post this threads it says that you cant post it here

any way thxs for ur help in advance

It is the right way to do it, only you messed up the names. If you have two Buttons in your layout (yes and no), you set an onClickListener on one of them calling <buttonName>.setOnClickListener(...);, so in this case yes.setOnClickListener(...). Also I would suggest renaming your Buttons in the code to bYes and bNo or something like that so you know it is a Button. And yes, it is the right section :good:
 

Masrepus

Senior Member
Feb 12, 2013
767
99
Dont you also have to declare the buttons with
Button yes = (Button)findViewById(R.id.byes);

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Dont you also have to declare the buttons with
Button yes = (Button)findViewById(R.id.byes);

If you do it this way, "yes" is only declared in the scope of the onCreate method. It is possible, but for views which sit in memory anyway it's more convenient to have the those delared as instance variables like he did above, right below the class declaration.
 
  • Like
Reactions: Masrepus

Masrepus

Senior Member
Feb 12, 2013
767
99
If you do it this way, "yes" is only declared in the scope of the onCreate method. It is possible, but for views which sit in memory anyway it's more convenient to have the those delared as instance variables like he did above, right below the class declaration.
oh didnt see that

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 

RahulRao66

Member
Jun 17, 2014
17
5
Pune
Dude I think u Have Interchanged the functions of Button yes and Button no.

yes = (Button)findViewByID(R.id.byes);
no = (Button)findViewByID(R.id.bno);

yes.setonClickListener(new View.OnClickListener() {

@override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});


no.setonClickListener(new View.OnClickListener() {

@override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});


Also Just For the sake of testing you can add a toast message as below

yes.setonClickListener(new View.OnClickListener() {

@override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast t = Toast.makeText(getApplicationContext(),"YES Pressed",Toast.LENGTH_SHORT);
t.show();
}
});


no.setonClickListener(new View.OnClickListener() {

@override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast t = Toast.makeText(getApplicationContext(),"NO Pressed",Toast.LENGTH_SHORT);
t.show();
}
});

Also Check the activity_starting_point.xml file if you have messed with the Button IDs.'

Hit Thanks If I have Helped you ......
 
Last edited:

surlac

Senior Member
Feb 1, 2012
58
6
Dont you also have to declare the buttons with
Button yes = (Button)findViewById(R.id.byes);
You don't have to cast to Button class in this particular case, View has already defined method "setOnClickListener".
And yes, you better declare field to keep reference and avoid additional findViewById() calls.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    Dont you also have to declare the buttons with
    Button yes = (Button)findViewById(R.id.byes);

    If you do it this way, "yes" is only declared in the scope of the onCreate method. It is possible, but for views which sit in memory anyway it's more convenient to have the those delared as instance variables like he did above, right below the class declaration.