This question already has answers here:
Can't resolve symbol 'setText' error
(4 answers)
Closed 4 years ago.
I would like to change the text in a button, but the setText method doesn't work, it marks it in red and says symbol not resolved. How can i fix this? Here is my code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
int partA = 9;
int partB = 9;
int correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer++;
int wrongAnswer2 = correctAnswer--;
}
TextView textObjectA = (TextView)findViewById(R.id.textPartA);
TextView textObjectB = (TextView)findViewById(R.id.textPartB);
Button buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
Button buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
Button buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//this part doesn't work
buttonObjectChoice1.setText("" + partA);
}
Move your code
TextView textObjectA = (TextView)findViewById(R.id.textPartA);
TextView textObjectB = (TextView)findViewById(R.id.textPartB);
Button buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
Button buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
Button buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//this part doesn't work
buttonObjectChoice1.setText("" + partA);
into onCreate() method.
Related
I am new to Android programming and tried to create a small App. I have a MainClass with one EditText and one TextField. EditText is for users to type in a number and the TextField shows the value of the input when clicking on the first button. The second button is for switching to the other class (MainClass2). Now i want to show the input number from EditText (that I defined as "number") in the next class MainClass2 in an empty TextField. I implemented an OnClickListener for the two different buttons mentioned in the MainClass (first screen). Since I only defined the input variable "number" when clicking the first button (cause "number" is then shown in the TextField as mentioned because it is only created via EditText when clicking the first button), "number" is not defined in the code of clicking the second button. Therefore I cant hand it over to the MainClass2 via Intent. Do you have any solutions for this? Thank you in advance. These are the codes for my Classes:
package com.example.teilnehmeranzahl;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
Button button2 = findViewById(R.id.button2);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(this); // calling onClick() method
Button two = (Button) findViewById(R.id.button2);
two.setOnClickListener(this);
}
public void onClick(View v) {
// default method for handling onClick Events..
switch (v.getId()) {
case R.id.button:
EditText editText2 = findViewById(R.id.editText2);
String name2=editText2.getText().toString();
int number=Integer.parseInt(name2);
TextView textView = findViewById(R.id.textView);
textView.setText(String.valueOf(number));
Random randomizer = new Random();
int name = randomizer.nextInt(number+1);
TextView textView2 = findViewById(R.id.textView2);
if (name == 1 ) {
textView2.setText("Player 1");
}
if (name == 2 ) {
textView2.setText("Player 2");
}
break;
case R.id.button2:
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("number",number); //here is the error cause number is not
defined, although it is..
startActivity(intent);
break;
default:
break;
}
}
}
And this is the code for MainClass2:
package com.example.teilnehmeranzahl;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView textView3 = findViewById(R.id.textView3);
Intent intent = getIntent();
String number = intent.getStringExtra("number");
textView3.setText(number);
}
}
In case R.id.button2: you have not set the value of number. So it will give number undefined error. To solve this issue either you have to initialize number globally or get the value of number in case R.id.button2: also. Like
case R.id.button2:
EditText editText2 = findViewById(R.id.editText2);
String name2=editText2.getText().toString();
int number=Integer.parseInt(name2);
Intent intent =
new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("number",number);
startActivity(intent);
break;
I am trying to make an simple button that will cooperate with TextView, array and method changer (in public class Change). However the application uses the method changer only twice (below). It should work on every click. The changer method is used for the change in displayed array.
1-click: 1 |
2-click: 4 |
3-click: 1 |
4-click: 1 |
...-click: 1 |
I don't know where is the problem.
My target solution should be 1,4,1,4,1,4 ...
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String[] numbers = {"1", "2","3","4"};
private Button mButton;
int i = 0;
Change ch = new Change();
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button2);
text = (TextView) findViewById(R.id.tv1);
ch = new Change();
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setText(numbers[0]);
numbers = ch.changer(numbers);
i++;
}
});
}
}
Change.java
public class Change {
String[] arraynew = new String[4];
public String[] changer(String[] array){
arraynew[0]=array[3];
arraynew[1]=array[2];
arraynew[2]=array[1];
arraynew[3]=array[0];
return arraynew;
}
}
The arraynew variable in your code is global - so it will not create a new array every time you call the changer method, only rearranges its contents.
Here comes the tricky part: When you call the method for the second time, the parameter is the same object as arraynew. So when you do arraynew[0]=array[3] it changes 4 to 1 on the first slot; but arraynew[3]=array[0] will copy that newly changed 1 to the third index (where it is still the same 1 you put there). It basically eliminates the 4, making it impossible to recover.
Solution: Make arraynew local. If you declare it in the method, it will never be the same array, so the issue won't appear.
I have a problem with a TextView showing letters instead of numbers.
I am trying to make a converter app.
Here is my code:
package com.rickhuisman.converter;
import android.icu.text.DecimalFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class LengthActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_length);
}
public void onClickConvertLength(View view) {
// variables
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1Length);
Spinner spinner2 = (Spinner)findViewById(R.id.spinner2Length);
Button button = (Button)findViewById(R.id.convertButtonLength);
// spinner 1
String spinner1Value = String.valueOf(spinner1.getSelectedItem());
// spinner 2
String spinner2Value = String.valueOf(spinner2.getSelectedItem());
EditText inputChecker = (EditText)findViewById(R.id.InputLength);
TextView showText = (TextView)findViewById(R.id.showTextLength);
String inputCheckerText = inputChecker.getText().toString();
EditText input = (EditText)findViewById(R.id.InputLength);
// If input == 0
if(inputCheckerText.equals("")) {
inputCheckerLength();
}
// Millimeters && Millimeters
else if(spinner1Value.equals("Millimeters") && spinner2Value.equals("Millimeters")) {
// Get the input
Double formule = new Double(input.getText().toString());
methodLength(formule);
}
// Millimeters && Centimeters
else if(spinner1Value.equals("Millimeters") && spinner2Value.equals("Centimeters")) {
// Get the input
Double formule = new Double(input.getText().toString());
methodLength(formule);
}
}
// If the input == 0
public void inputCheckerLength() {
TextView showText = (TextView)findViewById(R.id.showTextLength);
CharSequence toastText = "Nothing to convert!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, toastText, duration);
toast.show();
}
// The method
public void methodLength(double formule) {
TextView showText = (TextView)findViewById(R.id.showTextLength);
showText.setText(Double.valueOf(formule).toString());
}
}
image of the code: Code
The problem is that if I put to many numbers in the EditText the TextView will show letters, is there a way I can make the TextView show the whole number and not letters?
Image of the problem:Image
Sorry for the bad English and thank you!
The problem is that you are using the Double.toString() method to output your double value, which uses the scientific notation format.
If you want to display the plain number you can use String.format() instead:
showText.setText(String.format("%.0f", formule));
In case you want to show decimals you can use e.g. %.2f for two decimals instead of %.0f.
I'm a noob Android studio programmer (this is hour 2 of learning!) and I expect this is a real rookie error I'm making!
I've got a Plain Text field in my application and I would like to set the text of this dynamically. I've given the plain text field the ID of: "resultText". Here's what I try;
public void calcnums(View v)
{
int x=firstNum + seondNum;
resultText.setText("Result: " + x);
}
For some reason I get 'resultText' highlighted in red and the hover over message is; Cannot resolve symbol 'resultText'.
I have the feeling that I'm doing something wrong by using the ID, but I'm lost!
Full code as suggested in comments;
import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class AddNumbers extends AppCompatActivity {
private int firstNum;
private int seondNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_numbers);
}
public void calcnums(View v)
{
int x=firstNum + seondNum;
resultText.setText(String.format("Result: %d", x);
}
public void setNums(View v)
{
TextView tx= (TextView) findViewById(R.id.text);
Random r = new Random();
int x=r.nextInt(2) + 1; // r.nextInt(2) returns either 0 or 1
firstNum = x;
r = new Random();
x=r.nextInt(2) + 1;
seondNum = x;
num1.setText(""+firstNum);
num2.setText(""+seondNum);
}
}
It seems you need to declare the View resultText
Like,
EditText resultText = (EditText) findViewById(R.id.resultText);
Also make sure that the name you are using for the resultText View in xml is written correctly as provided in the method findViewById()
usually cannot resolve symbol means some problems in variable declarations.
I am too learning android and I stumbled upon this problem as well.
I don't have the whole snippet of your code.But the thing you might be missing is to point your EditText object to view in xml.
EditText resultText = (EditText) v.findViewById(R.id.result_edit_text);
<EditText
android:id="#+id/result_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/result_edit_text"
/>
Let's assume that we have an layout with an edittext
<EditText
android:id="#id/txt_user_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
It has an ID. In your actitivy you must find and cast the EditText as follow:
EditText txtUserEmail = (EditText) findViewById(R.id.txt_user_email);
txtUserEmail.setText("klaus.dieter#lusty-swingers.de");
You need to find your text view in onCreate(), like that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_numbers);
TextView tx= (TextView) findViewById(R.id.text);
}
i recently started programming in Android and i came across a little problem.
What i'm trying to do is:
I have NewsActivity and a NewsRows.class (in the same package). So the news activites just creates a new NewsRows object and tells it to fill the TableLayout with new rows.
It works fine as long as i try to add an image from a resource... The app just keeps crashing.
The debugger tells me it can't find the resource but i can't find out why!
My code is here:
News Acitivty
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.os.Bundle;
public class News extends Activity {
NewsRows rowClass = new NewsRows();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
NewsRows.createNewsEntries(this);
}
}
NewsRows.class
package de.myapp.app.activites.news;
import de.myapp.app.R;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class NewsRows {
static TextView title;
static TableRow tRow;
static TableLayout tLayout;
public NewsRows() {
}
public static void createNewsEntries(Activity contextActivity) {
ImageView image = new ImageView(contextActivity);
image.setBackgroundColor(R.drawable.myimage);
tLayout = (TableLayout) contextActivity.findViewById(R.id.NewsTable);
for(int a = 0; a < 100; a++) {
tRow = new TableRow(contextActivity);
title = new TextView(contextActivity);
//tRow.addView(image);
title.setText("This is a test.");
tRow.addView(title);
tLayout.addView(tRow);
}
}
}
EDIT:
The line
image.setBackgroundColor(R.drawable.myimage);<br />
Is actually supposed to be:
image.setImageResource(R.drawable.myimage);
You're trying to set an image into a background color:
Change this:
image.setBackgroundColor(R.drawable.myimage);
to this:
image.setBackgroundResource(R.drawable.myimage);
Fun fact:
TableLayout tLayout = (TableLayout) findViewById(R.id.NewsTable);
TableRow tRow = new TableRow(this);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.myimage);
tRow.addView(image);
tLayout.addView(tRow);
If i put this code right in the News.Activity it works...
You can't set a drawable for as a background color of your ImageView, that's why you have the resource not found exception on your logcat!!
image.setBackgroundColor(R.drawable.myimage);
change it to this:
image.setBackgroundResource(R.drawable.myimage);
Also try to change this
public static void createNewsEntries(Activity contextActivity)
with
public static void createNewsEntries(Context contextActivity)