I'm really new in android and java, but im trying to make an android app.
Im trying to make something were you can just type in your name and then it should view it by a push on a button. Eclipse is giving me the "unreachable code" error. I was wondering if you guys could see what i was doing wrong. The error is given in the two rules with final in front of it. If i remove one of these rules it will just move the error to the other rule.
Thank you in advance,
Marek
package com.tutorial.helloworld2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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.main, menu);
return true;
final EditText nameField = (EditText) findViewById(R.id.nameField);
final TextView nameView = (TextView) findViewById(R.id.nameView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nameView.setText("Hello " + nameField.getText());
}
});
}
}
Move return true to the end of method as the method execution will end on this statement and no code below will be executed.
it is due to return statement, return will return a true and exit the entire method you have currently written code in. you should write this in onCreate() like.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final EditText nameField = (EditText) findViewById(R.id.nameField);
final TextView nameView = (TextView) findViewById(R.id.nameView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nameView.setText("Hello " + nameField.getText());
}
});
}
now you will get your desired result
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Hi everybody i am started my first app on android studio and iam getting this error in "can't resolve symbole 'set.OnClickListener'" as shown in image below
please i made many searches in google to solve it but nothing found.
So please somebody help.
thanks.
package com.example.omar.app1;
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 android.view.View.OnClickListener;
import static android.view.View.OnClickListener;
public class Main 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.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);
}
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{startActivity(new Intent(Main.this,Second.class)); }
});
}
Move all the code relating to your Button inside onCreate() (after setContentView(), before the closing }).
My example is:
public class MainActivity extends Activity {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this,Second.class));
}
});
}
}
I hope this documentation will help you Button Class and setOnClickListener
All the codes related to button must be within the onCreate() method after the line : setContentView(R.layout.activity_main);
These lines :
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{startActivity(new Intent(Main.this,Second.class)); }
});
must be written within onCreate method after setContentView(R.layout.activity_main)
The error is Button b = (Button) findViewById(R.id.button1);
you declare your button and initialize it in compile time with
Button b = (Button) findViewById(R.id.button1);
and of course before, you crete the view is impossibile to find id without context.
you can attach any view after you set the content.
So you can change
// compile time
Button b = (Button) findViewById(R.id.button1);
in this
Button b;
and you can change your onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// now you can attach the view
b = (Button) findViewById(R.id.button1);
}
after this you can set your click listener in anonymous type like you did in next line or can still declare your listener in compile time and change this :
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{startActivity(new Intent(Main.this,Second.class)); }
});
in this:
View.OnClickListener myClickListener = new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent(Main.this,Second.class));
}
};
but now you need to set your OnClickListener, so your onCreate will:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// now you can attach the view
b = (Button) findViewById(R.id.button1);
b.SetOnClickListener(myClickListener);
}
Hope this help to understand.
I'm trying to implement onclicklistener but it isn't working on my phone or emulator.
here is the code:
package com.slaps.guess;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView tv;
Button one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.tvd);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
tv.setText("Anything");
break;
}
}
}
the text view is not changing to anything it is still.
note: button1 exists, and their is nothing wrong with my xml.
i want to implement because i have alot of button.
You are missing one.setOnClickListener(this) in your code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.button1);
one.setOnClickListener(this);
tv = (TextView) findViewById(R.id.tvd);
}
Before using listener you will have to add it in the object for which you want it.
I guess you want for the Button one here.
Use one.setOnClickListener(this) after one = (Button) findViewById(R.id.button1);
It looks like you're missing the required XML attribute in res/layout/activity_main.xml. Ensure it looks like:
<Button
android:id="#+id/button1"
onClick="onClick"
/>
The key being you have supplied the onClick attribute with the name of your method to invoke.
But if you want to set the click listener programmatically than you will need to use the Button.setOnClickListener method. If you do this your method signature will need to change to:
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
I'm relatively new to app development and very confused about what to do here. Please be very clear and very detailed in your responses, much appreciated!
I'm getting a host of errors from the following code block:
//Blank out the appropriate blanks
chkRect.setOnClickListener(new onClickListener() {
#Override
public void onClick(View v) {
if (chkRect.isChecked()){
chkCyl.setEnabled(false);
txtDiameter.setText("0");
txtDiameter.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtDiameter.setText("");
txtDiameter.setEnabled(true);
}
}});
chkCyl.setOnClickListener(new onClickListener(){
#Override
public void onClick(View v) {
if (chkCyl.isChecked()){
chkRect.setEnabled(false);
txtHeight.setText("0");
txtWidth.setText("0");
txtHeight.setEnabled(false);
txtWidth.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtHeight.setText("");
txtWidth.setText("");
txtHeight.setEnabled(true);
txtWidth.setEnabled(true);
}
}});
On both "onClickListener" lines and the 'public void' lines, the following shows up:
-onClickListener cannot be resolved as a type. (multiple lines)
-Method setOnClickListener(View.onClickListener) in the type View is not applicable for the arguments new onClickListener.
-The Method "onClick(View v)" must override or implement a supertype method.
Here are my imports and variable declarations:
import java.text.DecimalFormat;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
public class MaterialCalculator extends ActionBarActivity implements View.OnClickListener{
public Spinner materialDD;
public CheckBox chkRect;
public CheckBox chkCyl;
//public Spinner shapeDD;
DecimalFormat d = new DecimalFormat("#");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_material_calculator);
materialDD = (Spinner) findViewById(R.id.materialDD);
final CheckBox chkRect = (CheckBox) findViewById(R.id.chkRect);
final CheckBox chkCyl = (CheckBox) findViewById(R.id.chkCyl);
//shapeDD = (Spinner) findViewById(R.id.shapeDD);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Button btnClearAll = (Button) findViewById(R.id.btnClearAll);
final EditText txtDiameter = (EditText) findViewById(R.id.txtDiameter);
final EditText txtLength = (EditText) findViewById(R.id.txtLength);
final EditText txtWidth = (EditText) findViewById(R.id.txtWidth);
final EditText txtHeight = (EditText) findViewById(R.id.txtHeight);
final EditText edtTxtTotprice = (EditText) findViewById(R.id.edtTxtTotprice);
final EditText edtTxtWeight = (EditText) findViewById(R.id.edtTxtWeight);
I tried other solutions on this problem, including deleting the imports for view and onClickListener and using the ctrl+shift+O import, but it didn't work. Like I said, please be very specific with your answers since this is my first app and I'm not at all familiar with Eclipse to any great extent.
change
chkCyl.setOnClickListener(new onClickListener(){
with
chkCyl.setOnClickListener(new OnClickListener(){
and
chkRect.setOnClickListener(new onClickListener(){
with
chkRect.setOnClickListener(new OnClickListener(){
setOnClickListener takes as paramter an instance of a class that implements the OnClickListener interface
Maybe it can't get the variable from within the saved instance state? Try to define the variable right before the onClick Listener like this:
final CheckBox chkRect = (CheckBox) findViewById(R.id.chkRect);
chkRect.setOnClickListener(new onClickListener() {
#Override
public void onClick(View v) {
if (chkRect.isChecked()){
chkCyl.setEnabled(false);
txtDiameter.setText("0");
txtDiameter.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtDiameter.setText("");
txtDiameter.setEnabled(true);
}
}});
final CheckBox chkCyl = (CheckBox) findViewById(R.id.chkCyl);
chkCyl.setOnClickListener(new onClickListener(){
#Override`enter code here`
public void onClick(View v) {
if (chkCyl.isChecked()){
chkRect.setEnabled(false);
txtHeight.setText("0");
txtWidth.setText("0");
txtHeight.setEnabled(false);
txtWidth.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtHeight.setText("");
txtWidth.setText("");
txtHeight.setEnabled(true);
txtWidth.setEnabled(true);
}
}});
This code to change text when the user presses a button doesn't work. i tried to change it in some ways but i can't figure out why won't it change... please give me a bit help
package com.cookbook.simple_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class activity extends Activity {
private TextView txt = (TextView) findViewById(R.id.hello_text);
Button startButton = (Button) findViewById(R.id.trigger);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txt.setText(R.string.pressthisbutton);
}
});
}
}
Change it to
public class activity extends Activity {
private TextView txt;
Button startButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
txt = (TextView) findViewById(R.id.hello_text);
startButton = (Button) findViewById(R.id.trigger);
You need to initialize your Views after inflating your layout with setContentView(). Since your Views exist in your layout, they will return null if you haven't inflated your layout first. You can declare them before your setContentView() but you can't initialize them until after.
Also, since you are trying to access txt inside your listener it must either be final or declared as a member variable as above.
This was a rather easy one to spot but they aren't always. When you post a question try to describe what isn't working and how. Here it would be a NPE I'm guessing when you try to set the listener on your Button so it crashes. When it does crash, please provide the logcat so it is easier for us to spot the problem.
package com.cookbook.simple_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
final TextView txt = (TextView) findViewById(R.id.hello_text);
Button startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txt.setText("your text");
}
});
}
}
try this, I moved your button and textview declaring to the body of the activity.
android life cycle always starts with onCreate() method assign the id inside the onCreate
don't use private modifer for the TextView
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class activity extends Activity {
TextView txt;
Button startButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
txt = (TextView) findViewById(R.id.hello_text);
startButton = (Button) findViewById(R.id.trigger);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txt.setText(R.string.pressthisbutton);
}
});
}
}
Always initialize your textview and other widgets in oncreate method other wise youll get NullPointerException
Try this way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
txt = (TextView) findViewById(R.id.hello_text);
startButton = (Button) findViewById(R.id.trigger);
txt.setText("It Working Now!!");
I am having trouble linking my interface with my java.
package com.example.game;
import android.os.Bundle;
import android.R;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn1 = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <---- 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.main, menu); <--- main
return true;
}
}
I am having trouble with the layout. I can link my buttons ect. but it always throws an error on the generated code? why does it do this? I did the suggested fixes and ran it, which resulted into a crash... I am new to java and android development. I am learning as I go. I do have other experience in visual basic and what not.
Suggested fixes:
change to activity_list_ item
and can not be resolved.
Your btn1 assignment is incorrect. When you're assigning it on initialization, there is no view, as the code is run prior to onCreate. Instead, it should look like this:
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <---- activity_main
btn1 = (Button) findViewById(R.id.button1);
}