I am trying to learn Android. I want to build a simple letter counting app for counting random name, that is my code l have problem with onclickListerner. Thank you in advance Counting Themed Game
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("saya");
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("van");
}
return super.onOptionsItemSelected(item);
}
}
});
}
Replace this:
b.setOnClickListener() ;
With this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Your code here
}
});
And remove this:
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stu
}
Since you're extending the onclicklistener, you should call b.setOnClickListener(this);
Related
I have a first activity ("Home"), with two buttons: one is called About and leads to activity About and the second is named List and leads to the activity List.
Manifest.xml should be fine, but I get a load of tiny petty errors I can't fix up by myself, regrettably.
Home.class is the following
Public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button AboutButton = (Button)findViewById(R.id.About);
AboutButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view); {
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}
Button ListButton = (Button)findViewById(R.id.List);
ListButton.setOnClickListener(new View.onClickListener());{
#Override
public void onClick(View view); {
Intent openList = new Intent(Home.this, List.class);
startActivity(openList);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
while About.class is like this
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
public void onClick(View view); {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_about, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and List is like this:
public class List extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I get lot of red light bulbs, saying, for instance that ")" or ";" is expected or (worse) onClickListener cannot be resolved
Last but not least: I copied this code online and I was wondering why after "View" there is a "view"; what does it mean?
I copied your code and I see fails everywhere... let me explain you what's going on ...
HOME CLASS
1.- You have to remove the ")"
2.- You don't have to ";" when you call onClick()
3.- When you are don on your onClick() NOW you have to close it, you missed the ");"
AboutButton.setOnClickListener(new View.OnClickListener()){ //<-- Just remove one
#Override
public void onClick(View view); { //<-- Remove this ";"
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}//Here goes ");"
4.- The ListButton has the same issues so just fix it as you will fix the first one.
ABOUT CLASS
1.-On this case you have the setOnClickListener() ok, BUT why you have two onClick(View view)? It's not necessary just remove one of them.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button) findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
});
}
LIST CLASS
1.-Well in your List class you have made the same error as the first one... Your onClickListener() it's wrong.
2.-Once again you included an unnecessary ")" on new View.OnClickListener() just remove it,
3.-Another fail that I'm seeing is that you are trying to make an Intent but you are refering that you are on About.this and you are NOT. You are on List class so you have ot put List.this because the first parameter refers :
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
More information about Intents
4.- You need to close again the setOnClickListener()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){ //<-- remove one ")"
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class); //<-- Remove About.this and put List.this
startActivity(returnhome);
}
}//<-- Close the setOnClickListener() with ");"
}
It's okay guy, this is your first question and I'll answer it, but NOW as I've made the favor to take my time and explain to you what was wrong on your code take your time to :
How do I as a question on StackOverflow
Learn some Android basics
And the most IMPORTANT THING
DO NOT COPY PASTE AN INTERNET CODE if you don't know the basics, I mean you can copy paste the code, but you'll face with this problem every time you do this, so first of all read the tutorial, make an examples, and you'll improve every day.
I am new to QuickBlox. After going through the tutorial I have done the following to signup a new user into QuickBlox. The problem is that I am getting the following errors:
1. {"errors":["Token is required"]}
2. STATUS : 201
I referred to the following question Register user to QuickBlox Users from android but it didn't help.
My Codes:
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button sign_up;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QBSettings.getInstance().fastConfigInit("XxxXX", "XXXXXX",
"XXXXXX");
sign_up = (Button) findViewById(R.id.sign_up);
sign_up.setOnClickListener(MainActivity.this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.sign_up:
QBAuth.createSession(new QBEntityCallback<QBSession>() {
#Override
public void onError(List<String> arg0) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess() {
// TODO Auto-generated method stub
final QBUser user = new QBUser("user1", "123456789");
user.setExternalId("45345");
user.setFullName("User1");
user.setPhone("123456789");
QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
#Override
public void onSuccess(QBUser user, Bundle args) {
}
#Override
public void onError(List<String> errors) {
}
});
}
#Override
public void onSuccess(QBSession arg0, Bundle arg1) {
// TODO Auto-generated method stub
}
});
break;
default:
break;
}
}
}
Where did you get this error? It's not clear from your code example
Also the right way to create a user is inside this callback:
#Override
public void onSuccess(QBSession arg0, Bundle arg1)
not this
#Override
public void onSuccess()
I have created my dialog when I press one of the menu buttons:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_first__window, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id)
{
case R.id.action_settings:
About_us aboutus = new About_us(this);
aboutus.show();
return true;
case R.id.close: System.exit(0); return true;
}
return super.onOptionsItemSelected(item);
}
My Dialog layout
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Close"
android:onClick="ext_btn"/>
My Dialog Class
public class mydialog extends Dialog
{
public About_us(Context context) {
super(context);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about_us);
}
public void ext_btn(View view) {
About_us.this.dismiss();
}
}
I Have tried a lot of codes to make the close button dismiss the dialog.
My app always crashes.
Where is the mistake ?
Try this one.
Button btn_ext = (Button) findViewById(R.id.btn_ext);
btn_ext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
ok so now what i got is this as shown below . cant seem to solve it still . a little help please for android developer eclipse application for my final year project .wacthed alot of videos about it on youtube and google but non manage to help fixed the syntax error i've got here .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Category extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
};
Check your onClick() methods bro...
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
}
}); // This was missing.
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
}
});
Take the semicolon away at the end of this inner onClick method
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
Replace
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
with
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
}
You did not close the scope properly.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.lge.cptool.R;
public class Category extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
});
}
}
Nothing more than syntax error. Use this
public class Category extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
}
});
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am new to android development and I cannot understand this error, I am making a simple stop watch app, it has a Chronometer and 3 buttons (Start, stop, reset) I define all of these in the main java file correctly with their correct tags.
Its strange because I tested the app before and my app was working fine, but then I re-positioned the buttons in the GUI, and now it is saying that I am not casting the chronometer correctly.
06-06 19:11:07.488: E/AndroidRuntime(21511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shitstopwatch/com.example.shitstopwatch.ShittyClockActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Chronometer
here is the main java file:
public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
and here is the activity XML with the ID tags:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.shitstopwatch.ShittyClockActivity$PlaceholderFragment" >
<Chronometer
android:id="#+id/mainChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="#string/chronometer"
android:textSize="#dimen/BigText" />
<Button
android:id="#+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mainChronometer"
android:layout_below="#+id/mainChronometer"
android:layout_marginTop="63dp"
android:text="#string/startButton" />
<Button
android:id="#+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/startButton"
android:layout_below="#+id/startButton"
android:layout_marginTop="16dp"
android:text="#string/resetButton" />
<Button
android:id="#+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/resetButton"
android:layout_below="#+id/resetButton"
android:layout_marginTop="20dp"
android:text="#string/stopButton" />
</RelativeLayout>
Clean your project (e.g., Project > Clean in Eclipse).
Sometimes, the R constants get out of sync with the resource entries in the APK file, causing this error.