Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Android apps help
Android apps help [message #1006866] Sun, 03 February 2013 04:05 Go to next message
Tyler Mayoff is currently offline Tyler Mayoff
Messages: 2
Registered: February 2013
Junior Member
Hello,
I'm just experimenting with making apps on android and decided to start simple with making a button. I have encountered a problem with the code.


package com.example.minecraftcrafting;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;

Button button = (Button)findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You pressed button", Toast.LENGTH_SHORT);

}


}) <-------- Red underline

}

}


When I do what it tells me I get this


package com.example.minecraftcrafting;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;

Button button = (Button)findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() { <--- Whole line Red underlined

@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You pressed button", Toast.LENGTH_SHORT);

}


})

}

}

I am very new to this so can you please go easy on me.
Re: Android apps help [message #1007127 is a reply to message #1006866] Mon, 04 February 2013 14:46 Go to previous messageGo to next message
David Wegener is currently offline David Wegener
Messages: 1341
Registered: July 2009
Senior Member
Statements have to end with a ;
Re: Android apps help [message #1007143 is a reply to message #1006866] Mon, 04 February 2013 15:41 Go to previous messageGo to next message
sabih agbaria is currently offline sabih agbaria
Messages: 7
Registered: July 2012
Junior Member
button.setOnClickListener( *** );

here you are calling a method, normally you put a parameter in place of the "***" and put a semicolon ; after the closing parenthesis.

in this case you put an object you just create of an anonymous class. look this up in google for more info: anonymous class

Re: Android apps help [message #1007189 is a reply to message #1007127] Mon, 04 February 2013 21:24 Go to previous messageGo to next message
Tyler Mayoff is currently offline Tyler Mayoff
Messages: 2
Registered: February 2013
Junior Member
When I try that it tells me that button.setOnClickListener(****(); says that it is unreachable code
Re: Android apps help [message #1007201 is a reply to message #1007189] Mon, 04 February 2013 22:41 Go to previous message
Russell Bateman is currently offline Russell Bateman
Messages: 3194
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

On 2/4/2013 2:24 PM, Tyler Mayoff wrote:
> When I try that it tells me that button.setOnClickListener(****(); says
> that it is unreachable code

This isn't a Java forum or an Android forum; you're problem here has
been Java all along.

The reason the compiler tells you that it's unreachable code (after
fixing your first problem, the missing semicolon) is because you've coded:

return true;


smack in the middle of the method (and before the whole Button thing).

If you're following a tutorial, you haven't "squinted" hard enough and
have inserted the return by mistake. Yup, it's a little bewildering at
first (the whole anonymous class thing is a fuzzy syntax when you're not
used to it).

A really good tutorial for Android stuff can be had at
http://www.vogella.de.

Best of luck.
Previous Topic:Mark like words in Teradata plugin?
Next Topic:CDT and JDT - how?
Goto Forum:
  


Current Time: Sat Sep 20 20:23:55 GMT 2014

Powered by FUDForum. Page generated in 0.02873 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software