Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,806,815 Members 39,642 Now Online
XDA Developers Android and Mobile Development Forum

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

Tip us?
 
beefmaster
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Jun 2014
Prompt [help] i keep getting an error when i add an on click listener

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
Old
#2  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by beefmaster View Post
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
 
Masrepus
Old
#3  
Masrepus's Avatar
Senior Member
Thanks Meter 73
Posts: 659
Join Date: Feb 2013
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
Old
#4  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by Masrepus View Post
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.
The Following User Says Thank You to SimplicityApks For This Useful Post: [ Click to Expand ]
 
Masrepus
Old
#5  
Masrepus's Avatar
Senior Member
Thanks Meter 73
Posts: 659
Join Date: Feb 2013
Quote:
Originally Posted by SimplicityApks View Post
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
Old
(Last edited by RahulRao66; 22nd June 2014 at 08:50 PM.)
#6  
Junior Member
Thanks Meter 4
Posts: 16
Join Date: Jun 2014
Location: 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 ......
 
surlac
Old
#7  
Junior Member
Thanks Meter 1
Posts: 21
Join Date: Feb 2012
Quote:
Originally Posted by Masrepus View Post
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.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Schedule Is Up for xda:devcon ’14

With xda:devcon ’14 just around the corner, and the venue close to selling out, there is a lot … more

aLogcat Returns Thanks to XDA Love

The developers of Android, that is Google, gave users several great debugging tools that can be used in … more

And the Winners in the XDA Pebble Development Challenge Are…

Almost two months ago, we set out with Pebble to findthree amazing … more

MediaTek Releases Full Kernel Source for First Android One Devices

Those who might have thought that MediaTek wouldnever release working … more