Android apps help [message #1006866] |
Sat, 02 February 2013 23:05  |
Eclipse User |
|
|
|
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 #1007201 is a reply to message #1007189] |
Mon, 04 February 2013 17:41  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.10499 seconds