Chainfire Turns Your Bootanimation into a Logging Center

Having a nice boot animation certainly adds a little bit of aesthetic polish to your … more

Android TV Launcher Pushed to Google Play

Over the past decade, the tech universe has seen two drastic and widely contrasting changes with … more

Cyngn, OnePlus, Micromax – The Legal Battle

Recently, a battle has been waging in India over the rights to distribute the commercial … more

Lean Mean Battery Power Saving App Review

We talk a lot about battery topics here at XDA TV. We talk about everything from Power Banks to USB … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP beefmaster

16th June 2014, 02:37 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Jun 2014
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
16th June 2014, 01:24 PM   |  #2  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
346 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by beefmaster

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
16th June 2014, 08:59 PM   |  #3  
Masrepus's Avatar
Senior Member
Thanks Meter: 79
 
702 posts
Join Date:Joined: Feb 2013
More
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
17th June 2014, 11:25 AM   |  #4  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
346 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by Masrepus

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: [ View ]
18th June 2014, 01:51 PM   |  #5  
Masrepus's Avatar
Senior Member
Thanks Meter: 79
 
702 posts
Join Date:Joined: Feb 2013
More
Quote:
Originally Posted by SimplicityApks

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
22nd June 2014, 09:16 PM   |  #6  
Junior Member
Flag Pune
Thanks Meter: 5
 
17 posts
Join Date:Joined: Jun 2014
More
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 by RahulRao66; 22nd June 2014 at 09:50 PM.
23rd June 2014, 04:01 AM   |  #7  
Member
Thanks Meter: 3
 
36 posts
Join Date:Joined: Feb 2012
Quote:
Originally Posted by Masrepus

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.

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes