Best Alarm App for Android?

There are tons of choices to choose from when looking for a great alarm app for Android. While the stock … more

Unofficial CM12.1 Available for Moto E 2014

While Motorola is yet to release a final 5.0.2 build for the Moto E 2014, XDA Senior … more

Pinsy Brings Social Sketching To Your Watch & Phone

Did you watch Apple’s VP draw on his wrist during the Apple Watch announcement … more

CyanogenMod 12.1 for Samsung Galaxy S5

Android 5.1 is slowly putting its mark in the custom ROM world. CyanogenMod 12.1 is available to … more
Post Reply Subscribe to Thread Email Thread

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

16th June 2014, 01: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, 12:24 PM   |  #2  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 340
 
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, 07:59 PM   |  #3  
Masrepus's Avatar
Senior Member
Thanks Meter: 89
 
733 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, 10:25 AM   |  #4  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 340
 
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, 12:51 PM   |  #5  
Masrepus's Avatar
Senior Member
Thanks Meter: 89
 
733 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, 08: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 08:50 PM.
23rd June 2014, 03:01 AM   |  #7  
Member
Thanks Meter: 4
 
47 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