07-25 10:15:37.960: E/AndroidRuntime(8661): android.content.res.Resources$NotFoundException: String resource ID #0x7
07-25 10:15:37.960: E/AndroidRuntime(8661): at android.content.res.Resources.getText(Resources.java:230)
Good day to all.
I am trying to display an integer value in a Text View and the above error shows up in LogCat.
There are other similar posts about this issue; like this, this, and this, but none of the solutions worked for me.
Any other ideas of what the problem might be?
Edited for code:
private static Button btnCancel;
private static Button btnConfirm;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtRoomNumber = (EditText)findViewById(R.id.txtRoomNumber);
btnCancel = (Button)findViewById(R.id.btnCancel);
btnConfirm = (Button)findViewById(R.id.btnConfirm);
btnCancel.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
System.exit(0);
}
});
btnConfirm.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int rmNo = getRoomNumberValue();
txtTesting.setText(rmNo);
}
});
}
private int getRoomNumberValue()
{
int temp = 0;
try
{
temp = Integer.parseInt(txtRoomNumber.getText().toString());
}
catch(Exception e)
{
e.printStackTrace();
}
return temp;
}
If you are trying to display an integer value in a TextView, use this:
myTextView.setText("" + 1); // Or whatever number
The error happens because TextView has another method: setText(int resid). This method looks for a resource id, which does not exist in your case. Link
You are trying to set the content text of a TextView with an integer value.
The issue is that the method you are using is expecting a resource id.
You need to make a String out of your integer before putting it in the TextView :
textView.setText(Integer.toString(7));
Change your Integer to String
textview.setText(String.valueOf(valueofint));
To convert integer to string use
int x=10;
Integer.toString(x);
That will solve your problem
Related
It's very simple code but when I initialize the TextView, the app just crashes.
I'm a beginner, so I don't know if I made something wrong.... but in my opinion the code looks fine. Android Studio doesn't report any errors either.
int counterint = 0;
TextView counter = findViewById(R.id.countertv);
public void pressthebutton(View view){
counterint++;
counter.setText(counterint);
}
You are probably setting the TextView during the class instantiation. You should update your code as follows:
int counterint = 0;
TextView counter;
public void onCreate (Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(<your_layout>);
// Set the textView only after setContent.. Otherwise, findViewById will return null
counter = findViewById(R.id.countertv);
}
public void pressthebutton(View view){
counterint++;
counter.setText(Integer.toString(counterInt));
}
try setting your textview this way :
public void pressthebutton(View view){
counterint++;
counter.setText(String.valueOf(counterInt));
}
The error causes because you directly assign an integer value to the TextView. It is good to convert an integer or any data type to String when you assigning it to the TextView
int counterint = 0;
TextView counter = findViewById(R.id.countertv);
public void pressthebutton(View view){
counterint++;
counter.setText(Integer.toString(counterint));
}
otherview you can convert the Integer value to string first and then assign it to the TextView as follows
int counterint = 0;
TextView counter = findViewById(R.id.countertv);
String counterString = Integer.toString(counterint)
public void pressthebutton(View view){
counterint++;
counter.setText(counterString);
}
Try this
counter.setText(counterint+"");
It automatically set the string value to textview
You may try this :
public void pressthebutton(View view){
counter.setText(String.valueOf(++counterint));
}
Hello I am very new in android
I am trying to get integer value from an EditText, But When I am parsing string to Integer I got NumberFormatException.
Please help me to come out of this error.
thanks in advance.
Program is:
int day,month,year;
EditText expense,ammount;
String[] exp=new String[10];
int[] amt=new int[10];
int count=0;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Calendar cal=Calendar.getInstance();
day=cal.get(Calendar.DAY_OF_MONTH);
month=cal.get(Calendar.MONTH)+1;
year=cal.get(Calendar.YEAR);
final TextView txtdate=(TextView)findViewById(R.id.txtdate);
expense=(EditText)findViewById(R.id.exp);
ammount=(EditText)findViewById(R.id.amnt);
final Button add=(Button)findViewById(R.id.btnadd);
final Button cancel=(Button)findViewById(R.id.btncancel);
final Button done=(Button)findViewById(R.id.btndone);
txtdate.setText(day+"/"+month+"/"+year);
add.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
getval();
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
clean();
}
});
done.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
total();
getval();
clean();
final TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Integer.toString(total()));
}
private int total() {
int total = 0;
// TODO Auto-generated method stub
for(int i=0;i<=count;i++)
{
total+=amt[i];
}
return total;
}
});
}
protected void clean() {
// TODO Auto-generated method stub
expense.setText(" ");
ammount.setText(" ");
}
protected void getval() {
// TODO Auto-generated method stub
final Editable e2=expense.getText();
final Editable e1=ammount.getText();
final int i=Integer.parseInt(e1.toString());
amt[count]=i;
exp[count]=e2.toString();
System.out.println(amt[count]);
System.out.println(exp[count]);
count++;
}
}
Exception is:
java.lang.NumberFormatException: unable to parse ' 600' as integer
Remove any leading or trailing spaces from the number first:
int inputNumber = Integer.parseInt(editText.getText().toString().trim());
Alternatively, you can remove all non-numeric characters from the string using regular expressions:
String cleanInput = editText.getText().toString().replaceAll("[^\\d]", "");
int inputNumber = Integer.parseInt(cleanInput);
Though if non-numeric input characters is a problem you'd probably want to restrict the EditText to numeric only. See this question. It says to add the following attribute to the EditText:
android:inputType="number"
You have a space in your integer.
Add the following attribute to your EditText in your xml to only allow entering integers:
android:inputType="number"
Try this..
final int i=Integer.parseInt(e1.toString().trim());
bacause there is a space before that number see ' 600' that's why that error..
Hope this will help..
use this code
final int f = -1; // or other invalid value
if (edittext.getText().toString().length() > 0)
f = Integer.parseInt(edittext.getText().toString());
Until three weeks ago I knew nothing about developing applications for android, and with very basic Java background.
However, I took up the interest, and I'm now tying to develop a simple application that performs basic arithmetic operations.
What I would like the application to do is take in figures input by the user, up to 20 pairs, multiply each pair separately, and then add the results.
I have gotten through the multiplication of the pairs, but can't get onto adding up the results.
This is what I have so far for the multiplication part, and it works okay....
public class CalcAlgorithm extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
final String LOG_TAG = "MainScreen";
super.onCreate(savedInstanceState);
setContentView(R.layout.calc_algorithm);
final EditText value1=(EditText) findViewById(R.id.value1);
final EditText value2=(EditText) findViewById(R.id.value2);
final EditText value3=(EditText) findViewById(R.id.value3);
final EditText value4=(EditText) findViewById(R.id.value4);
final EditText value5=(EditText) findViewById(R.id.value5);
final EditText value6=(EditText) findViewById(R.id.value6);
final EditText value7=(EditText) findViewById(R.id.value7);
final EditText value8=(EditText) findViewById(R.id.value8);
final EditText value9=(EditText) findViewById(R.id.value9);
final EditText value10=(EditText) findViewById(R.id.value10);
final TextView result=(Button) findViewById (R.id.multiplyValues);
final TextView result2=(Button) findViewById (R.id.multiplyValues2);
final TextView result3=(Button) findViewById (R.id.multiplyValues3);
final TextView result4=(Button) findViewById (R.id.multiplyValues4);
final TextView result5=(Button) findViewById (R.id.multiplyValues5);
final TextView result6=(Button) findViewById (R.id.addButton);
Button multiplyButton = (Button)findViewById(R.id.multiplyValues);
multiplyButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
try {
int val1=Integer.parseInt(value1.getText().toString());
int val2=Integer.parseInt(value2.getText().toString());
Integer answer = val1*val2;
result.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to multiply numbers",e);
}
}
});
Button multiplyButton2 = (Button) findViewById(R.id.multiplyValues2);
multiplyButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val3 = Integer.parseInt(value3.getText().toString());
int val4 = Integer.parseInt(value4.getText().toString());
Integer answer = val3 * val4;
result2.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to multiply numbers",e);
}
}
});
Button multiplyButton3 = (Button) findViewById(R.id.multiplyValues3);
multiplyButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val5 = Integer.parseInt(value5.getText().toString());
int val6 = Integer.parseInt(value6.getText().toString());
Integer answer = val5 * val6;
result3.setText(answer.toString());
} catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers",e);}}
});
Button multiplyButton4 = (Button) findViewById(R.id.multiplyValues4);
multiplyButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val7 = Integer.parseInt(value7.getText().toString());
int val8 = Integer.parseInt(value8.getText().toString());
Integer answer = val7 * val8;
result4.setText(answer.toString());
} catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers",e);}}
});
Button multiplyButton5 = (Button) findViewById(R.id.multiplyValues5);
multiplyButton5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
int val9 = Integer.parseInt(value9.getText().toString());
int val10 = Integer.parseInt(value10.getText().toString());
Integer answer = val9 * val10;
result5.setText(answer.toString());
} catch (Exception e) {
Log.e(LOG_TAG,"Failed to multiply number",e);
}
}
});
Is this the right path? And, i realize that there is a way of summarising the code into fewer lines and not as i have it. Could someone please show me how I can go about having fewer lines of code, and then adding up the results all at once?
After adding the values, I'd again like to perform some more (basic) operations on the results, but i think after I have known how to add the results, I should be able to figure my way from there.
**Perhaps i should point out that i would require the addition to be done once separately i.e on clicking of an added button (TotalSum,maybe) separately and not progressively as the multiplication goes on, such that the user can see the multiplied results for each pair of figures entered, and the total of all paired results on clicking a separate button..
It would also be of immense help if i could get links to some books/documentation/videos that could help with arithmetic functions in writing android applications.
Any help will be greatly appreciated guys. :)
You could add a calculateResultSum() method, which you call at the end of each onClick method with does something like
void calculateResultSum() {
Integer res1 = Integer.parse(result.getText.toString());
// .. get all the other results
Integer finalResult = res1 + res2 + res2 + res4 + res5;
//do something with the result
}
BUT your approach is very redundant, i.e. you have the same code in all of you onClick methods. This is considered bad code style, you should try to extract the actual processing of the numbers into one single method and call this method from each of the listeners, for example
void addNumbers(TextView tv1, TextView tv2, TextViev res){
try {
Integer val1=Integer.parseInt(value1.getText().toString());
Integer val2=Integer.parseInt(value2.getText().toString());
Integer answer = val1*val2;
res.setText(answer.toString());
} catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers",e);}}
}
and set the onClicks to
multiplyButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addNumbers(value1,value2,result);
calculateResultSum();
});
with the corresponding views for each Button. Hope I could help.
If I understood correctly, here is the solution.
Create a class variable like private int total=0; and in each setOnClickListener add your answers to this variable.
Integer answer = val1*val2;
total += answer;
Also instead of using 5 Button and 10 TextViews you can do this with simply one Button and one TextView storing results in another TextView but you said you are practicing, if it is just to practice it should be alright.
package com.example.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText num1_edt=(EditText)findViewById(R.id.num1_edt);
final EditText num2_edt=(EditText)findViewById(R.id.num2_edt);
final Button add_btn=(Button)findViewById(R.id.add_btn);
findViewById(R.id.sub_btn);
findViewById(R.id.mul_btn);
findViewById(R.id.div_btn);
add_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int a=Integer.parseInt(num1_edt.getText().toString());
int b=Integer.parseInt(num2_edt.getText().toString());
Integer n=a*b;
Toast.makeText(getApplicationContext(),"Your Num Is:"+n.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
I'm new to programming and Android. I'm making my best attempt at a simple app and I'm stuck!
I have two editTexts (set to accept numbers only) and a button. The idea is to display the sum of the two user inputs when the button is pressed. However, my app stops working and force closes when I click the button in the emulator.
Any help would be greatly appreciated.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prevailing_torque);
Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
calculatePrevailing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
EditText editTextRecommended = (EditText) findViewById(R.id.editTextReccomended);
int p = Integer.parseInt(editTextPrevailing.getText().toString());
int r = Integer.parseInt(editTextRecommended.getText().toString());
prevailingSetting.setText(r+p);
}
});
}
I've looked at several similar questions on here but I haven't been able to find anything that I can implement further than what I have already done. But I am probably looking in the wrong places.
Thanks!
You can't use setText() with an Integer since it expects a CharSequence, you have to use
prevailingSetting.setText(String.valueOf(r+p));
or just
prevailingSetting.setText(""+(r+p));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prevailing_torque);
Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
EditText editTextRecommended =(EditText)findViewById(R.id.editTextReccomended);
calculatePrevailing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int p = Integer.parseInt(editTextPrevailing.getText().toString());
int r = Integer.parseInt(editTextRecommended.getText().toString());
int s = r+p;
prevailingSetting.setText(Integer.toString(s));
}
});
Ok ! if this is your problem then first of all convert your editText value to string and after that convert it into integer like !
String str1=editTextPrevailing.getText().toString();
String str2=editTextRecommended.getText().toString();
int p = Integer.parseInt(str1);
int r = Integer.parseInt(str2);
int s = r+p;
prevailingSetting.setText(""+s);
Can anyone please tell why the if statement never gets executed even though I enter ran in the edit text field and press ok? When the user enters ran and presses the button ok, I want another button to become visible which enables him to stop the current activity. Basically I want to know why the if is skipped.
public class Reciever extends Activity{
protected static final String TAG = null;
private Button ok,stp;
private TextView tv;
private EditText ev;
private String s1,s2,s3,s4;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
s1="nar";
setContentView(R.layout.stop);
tv=(TextView) findViewById(R.id.textView1);
tv.setText(s1);
ev=(EditText) findViewById(R.id.editText1);
ok=(Button) findViewById(R.id.ok_button);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
s2="ran";
tv.setText(ev.getText().toString());
s3=ev.getText().toString();
if(s3==s2)//not going inside this loop
{
stp=(Button) findViewById(R.id.stopb);
stp.setVisibility(View.VISIBLE);
stp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
});
}
}
if(s3==s2)//no
just replace the above line with below
if(s3.equalsIgnoreCase(s2))//no
Use .equals instead of == to compare the value of two strings.
if (s2.equals(s3))
Using == tests for reference equality. Two strings can contain the same characters but have different references.
You can't compare text with ==, you need to use equals.
if(s3.equals(s2))