Android EditText to int conversation error [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
This probably the most asked question, but it very difficult to find some answers. First I am newbie. I want to make simple quadratic equation formula app. That would allow to to find solution fast. I bump with the problem that Android Studio say Code is okey, but device crashes after opening app.
private Button mButton;
private EditText mEdit;
private EditText mEdit1;
private EditText mEdit2;
private TextView mText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mButton = (Button)findViewById(R.id.button2);
mEdit = (EditText)findViewById(R.id.editText);
mEdit1 = (EditText)findViewById(R.id.editText2);
mEdit2 = (EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.textView4);
//Int definēšana
final int i1 = Integer.parseInt(mEdit.getText().toString());
final int i2 = Integer.parseInt(mEdit1.getText().toString());
final int i3 = Integer.parseInt(mEdit2.getText().toString());
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Mainīgo ievade
int a = i1;
//Pārbauda vai a nav nulle
if (a == 0){
mText.setText(String.valueOf("Nav kvadrātvienādojums"));
} else {
int b = i2;
int c = i3;
//Diskriminanta aprēķināšana
double diskr = (b*b)-4*a*c;
//Kvadrātsakne no diskriminanta
double sd = (double) Math.sqrt(diskr);
//Sakņu aprēķināšana
double x1 = (-b+sd)/(2*a);
double x2 = (-b-sd)/(2*a);
//Rezultāta izvade
if (diskr < 0){
mText.setText(String.valueOf("Kvadrātvienādojumam nav sakņu"));
} else if (diskr == 0){
mText.setText(String.valueOf("Kvadrātvienādojumam ir viena sakne: " + x1));
} else {
mText.setText(String.valueOf("Kvadrātvienādojuma saknes: " + x1 + " un " + x2));
}
}
}
});
}
And logcat that may help detect a problem
FATAL EXCEPTION: main
Process: com.homemade.prtbust.kvadratvienadojums, PID: 4552
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.homem`enter code here`ade.prtbust.kvadratvienadojums/com.homemade.prtbust.kvadratvienadojums.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.homemade.prtbust.kvadratvienadojums.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

I think you miss
setContentView(R.layout.activity_main); " activity_name is the name of layout where is the edit text"
setContentView must be called before using findViewById

You are not assigning any layout to the Activity, therefore all your EditTexts, Button and TextView are not present, when you try to access any of them by code the program will crash.
Second, you should read the values for your variables when clicking the button, not in the oncreate, because that will cause you another error given that you'll try to parse something that is an empty string (unless you have set a value in the android:text tag in the xml).
I also wouldn't declare them as final (because you won't be able to modify them later.
Try using this code:
private Button mButton;
private EditText mEdit;
private EditText mEdit1;
private EditText mEdit2;
private TextView mText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button2);
mEdit = (EditText)findViewById(R.id.editText);
mEdit1 = (EditText)findViewById(R.id.editText2);
mEdit2 = (EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.textView4);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Int definēšana
int i1 = Integer.parseInt(mEdit.getText().toString());
int i2 = Integer.parseInt(mEdit1.getText().toString());
int i3 = Integer.parseInt(mEdit2.getText().toString());
//Mainīgo ievade
int a = i1;
//Pārbauda vai a nav nulle
if (a == 0){
mText.setText(String.valueOf("Nav kvadrātvienādojums"));
} else {
int b = i2;
int c = i3;
//Diskriminanta aprēķināšana
double diskr = (b*b)-4*a*c;
//Kvadrātsakne no diskriminanta
double sd = (double) Math.sqrt(diskr);
//Sakņu aprēķināšana
double x1 = (-b+sd)/(2*a);
double x2 = (-b-sd)/(2*a);
//Rezultāta izvade
if (diskr < 0){
mText.setText(String.valueOf("Kvadrātvienādojumam nav sakņu"));
} else if (diskr == 0){
mText.setText(String.valueOf("Kvadrātvienādojumam ir viena sakne: " + x1));
} else {
mText.setText(String.valueOf("Kvadrātvienādojuma saknes: " + x1 + " un " + x2));
}
}
}
});
}

try write this code ofter you set the content view for the activity
//load all the views from the xml file
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button2);
mEdit = (EditText)findViewById(R.id.editText);
mEdit1 = (EditText)findViewById(R.id.editText2);
mEdit2 = (EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.textView4);
//Int definēšana
final int i1 = Integer.parseInt(mEdit.getText().toString());
final int i2 = Integer.parseInt(mEdit1.getText().toString());
...
mButton.setOnClickListener(/*your code here*/);
you cant get the text from EditText before the EditText view created.
if this not solved youre problem, try write your i1/2/3 variables at onStart
#Override
protected void onStart() {
super.onStart();
final int i1 = Integer.parseInt(mEdit.getText().toString());
final int i2 = Integer.parseInt(mEdit1.getText().toString());
final int i3 = Integer.parseInt(mEdit2.getText().toString());
}

Related

Getting error in main activity on my basic calculator app

I'm creating a simple android calculator app. There is no compile time error, but when I'm tapping on any button, the app crashes.
package com.buckydroid.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView screen;
private String str1,str2,str3,result,str,sign;
private Double a,b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView)findViewById(R.id.textview);
}
private void onClick(View v){
Button button = (Button) v;
str += button.getText().toString();
screen.setText(str);
a = Double.parseDouble(str);
str = "";
}
private void onClickSigns(View v){
Button button = (Button) v;
sign = ((Button) v).getText().toString();
screen.setText(sign);
str="";
}
private void calculate(View v){
Button button = (Button) v;
str2 = screen.getText().toString();
b = Double.parseDouble(str2);
if (sign .equals("+")){
result = a+b+"";
}
else if (sign .equals("-")){
result = a-b+"";
}
else if (sign .equals("X")){
result = a*b+"";
}
else if (sign .equals("÷")){
result = a/b+"";
}
else{
result = "Something went wrong";
}
screen.setText(result);
}
}
Error Log
10-08 19:44:59.361 19449-19449/com.buckydroid.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.buckydroid.myapplication, PID: 19449
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
So if you need any other code then please comment
Thank you in advance..
Ok I got it after building in Android studio. The error is a lot bigger than what you pasted and I was able to track it down with the stack trace.
Basically you are trying to concatenate a string to a null with this line in your onClick method: str += button.getText().toString();
Since you can't do that you can fix the issue by replacing:
str += button.getText().toString();
With this:
str = button.getText().toString();
OR
Since this is a calculator app, you can initialize str in the onCreate method with str = ""; and then also remove str = ""; from the onClick method.
Full Code here:
public class MainActivity extends AppCompatActivity {
private TextView screen;
private String str1,str2,str3,result,str,sign;
private Double a,b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView)findViewById(R.id.textview);
str = "";
}
public void onClick(View v){
Button button = (Button) v;
str += button.getText().toString();
screen.setText(str);
a = Double.parseDouble(str);
}
public void onClickSigns(View v){
Button button = (Button) v;
sign = ((Button) v).getText().toString();
screen.setText(sign);
str="";
}
public void calculate(View v){
Button button = (Button) v;
str2 = screen.getText().toString();
b = Double.parseDouble(str2);
if (sign .equals("+")){
result = a+b+"";
}
else if (sign .equals("-")){
result = a-b+"";
}
else if (sign .equals("X")){
result = a*b+"";
}
else if (sign .equals("÷")){
result = a/b+"";
}
else{
result = "Something went wrong";
}
screen.setText(result);
}
}
onClick should be public not private.
also initialize your strings to empty string. Like:
private String str1 = "",str2="",str3="",result="",str="",sign="";
Hope this fixes your issue.
Way 1:
in your main_activity xml file add android:onclick="onClick" and make onClick(View view) method public.
for example
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:onClick="onClick"/>
And in main_activity.java file
public void onClick(View view){
}
Hope it help you :) Happy coding..

Cannot pass an Integer in an Intent [duplicate]

This question already has an answer here:
How to receive an int through an Intent
(1 answer)
Closed 4 years ago.
I'm trying to pass an Integer (from an edittext) to another activity through an intent.
When the user clicks a button, the text in the edittext will transform into a string and then into an int, then the int will be sent through an intent to another activity, but i have to use the int after that.
Here the activity sending the intent:
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
EditText conttext = (EditText) findViewById ( R.id.texthome );
Button buttone = (Button) findViewById(R.id.buttone);
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
View.OnClickListener maxim = new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
Here the activity receiving it:
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
int wall = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
wall = getIntent().getIntExtra("maxPressed", -1);
randone = (int) (Math.random()*10+1);
contone = 0;
}
Here i'm using it:
public void guessone (View view){
contone++;
textcontone.setText(getString(R.string.attempts) + "" + contone);
if (contone >= wall ){
resultaone.setText("You Failed" + " " + wall);
Toast.makeText(this, "You Failed", Toast.LENGTH_LONG).show();
}
When i use the app, the value of the int is always -1. Where i am wrong.
You can't use findViewById without setting the xml to the activity. That means you need to use findViewById method only after you have called setContentView.
Also you need to read the EditText text value once you click on the button otherwise it always will be null/empty.
Do this
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
EditText conttext;
Button buttone;
String maxom;
int maxam = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
View.OnClickListener maxim = new View.OnClickListener() {
#Override
public void onClick (View view) {
maxom = conttext.getText().toString();
maxam = Integer.parseInt(maxom);
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
Problem 1
Put this in the on click listener instead:
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
You want the values to be read at the time you click the button not when you open the activity, correct?
Problem 2
The following needs to be after setContentView in onCreate:
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
Keep the declarations where they are. Just the declarations:
EditText conttext;
Button buttone;
Note
Follow the same pattern in all your activities. Declare views as field variables, assign them in onCreate after setContentLayout. Get the values at the time they're needed.
public int getIntExtra (String name, int defaultValue)
Added in API level 1 Retrieve extended data from the intent.
Parameters name The name of the desired item. defaultValue the value
to be returned if no value of the desired type is stored with the
given name. Returns the value of an item that previously added with
putExtra() or the default value if none was found. See Also
putExtra(String, int)
This means that no int was found when you called getIntExtra(valueName, defaultValue); so the default value was chosen.
You should check to see what your maxam value is before you call the new activity.
In the activity you receive it:
wall = getIntent().getIntExtra("maxPressed");
SOLVED: by getInt and String.valueOf
private static final String IMGID = "ImgID";
if (getIntent().getExtras().containsKey(IMGID)) {
//Picasso.with(this).load(getIntent().getExtras().getString(IMG)).into(mImg);
Picasso.with(this).load(getIntent().getExtras().getInt(String.valueOf(IMGID))).into(mImg);
}

java.lang.NumberFormatException: Invalid double: "" [duplicate]

This question already has answers here:
java.lang.numberformatexception: invalid double: " "
(6 answers)
Closed 8 years ago.
When I try running my Android App, I get an error saying: java.lang.NumberFormatException: Invalid double: "". This is my code:
public class MainActivity extends Activity {
double score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (EditText) findViewById(R.id.gpa);
String gpaString = gpa.getText().toString();
final double gpaDouble = Double.parseDouble(gpaString);
sat = (EditText) findViewById(R.id.sat);
String satString = sat.getText().toString();
final int satInt = Integer.parseInt(satString);
act = (EditText) findViewById(R.id.act);
String actString = act.getText().toString();
final int actInt = Integer.parseInt(actString);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(actInt/36>satInt/2400){
score = (0.6*gpaDouble*25)+(0.4*(actInt/36)*100);
}else{
score = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
}
}
});
}
}
I essentially want to get numbers from three EditTexts, make one of them into a double and the other two into ints. Then I would use these variables to set the value for another double variable. I am not getting errors before I run the app. I feel that when the EditText field is blank, it will not parse correctly, but I am unsure how to solve this. What is the problem?
Invalid double: "".
you are parsing onCreate() value without putting any default value so the exception
final double gpaDouble = Double.parseDouble(gpaString);
because "" (empty String is not Double)

How do I set intent data to current activity editText?

I'm trying to transfer two numerical inputs from one activity to another's UI but when I click the button to change intent it crashes.
I get the following in logcat: http://pastebin.com/zkWPcSNZ , which suggests a problem with the way I parsed the data to the editTexts in CalcResult.
My question is what is wrong with the way I'm trying to pass the data to the CalcResult editText.Is there an alternative method of acheiving this?
My two classes look like this for reference:
public class MainActivity extends Activity implements OnClickListener {
//variables for xml objects
EditText offsetLength,offsetDepth,ductDepth;
Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting the variables to the xml id's and setting the click listener on the calc button
offsetLength = (EditText)findViewById(R.id.offLength);
offsetDepth = (EditText)findViewById(R.id.offDepth);
ductDepth = (EditText)findViewById(R.id.ductDepth);
calculate = (Button)findViewById(R.id.calc);
calculate.setOnClickListener(this);//don't cast the listener to OnClickListener
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String getoffsetlength = offsetLength.getText().toString();
String getoffsetdepth = offsetDepth.getText().toString();
String getductdepth = ductDepth.getText().toString();
double tri1,tri2;
double marking1,marking2;
double off1 = Double.parseDouble(getoffsetlength);
double off2 = Double.parseDouble(getoffsetdepth);
double off3 = Double.parseDouble(getductdepth)
;
marking1 = Math.pow(off1,2) + Math.pow(off2,2);
tri1 = (float)off2/(float)off1;
tri2 = (float)off3/Math.atan((float)tri1);
marking2 = (float)off3/Math.atan(tri2);
Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
myIntent.putExtra("numbers", marking1);
myIntent.putExtra("numbers", marking2);
startActivity(myIntent);
} catch (NumberFormatException e) {
// TODO: handle exception
System.out.println("Must enter a numeric value!");
}
}
}
This is the activity that I'm passing the data to:
public class CalcResult extends MainActivity
{
EditText result1,result2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
double mark1 = bundle.getDouble("number1");
double mark2 = bundle.getDouble("number2");
int a = Integer.valueOf(result1.getText().toString());
int b = Integer.valueOf(result2.getText().toString());
result1.setText(a + " ");
result2.setText(b + " ");
}
}
When you start your second activity, both editText are empty
So when you do :
int a = Integer.valueOf(result1.getText().toString());
int b = Integer.valueOf(result2.getText().toString());
it's equivalent to :
int a = Integer.valueOf("");
int b = Integer.valueOf("");
which throws the exception
Caused by: java.lang.NumberFormatException: Invalid int: ""
If you want to set them the values you passed through both activities, you can just do :
double mark1 = bundle.getDouble("number1");
double mark2 = bundle.getDouble("number2");
result1.setText(mark1 + " ");
result2.setText(mark2 + " ");
The error is that you are putting your extras with same key "numbers" , and you are trying to retreive them by another keys "number1" and "number2". your code should be like this :
Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
myIntent.putExtra("number1", marking1);
myIntent.putExtra("number2", marking2);
and to retreive them you should use :
Intent intent = getIntent();
double mark1 = intent.getDoubleExtra("number1", 0);
double mark2 = intent.getDoubleExtra("number2", 0);
And then , set the variables on your EditTexts like this :
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
result1.setText(mark1+"");
result2.setText(mark2+"");
you are using the same name for your extras when sending the intent, try correcting them.

How to create a scoreboard with a textview, 2 variables and a button in java for Android

I know this is a simpleton question, but I am not going to school for java, just learning it online.
how to a have a textview with an initial value of 0. and then everytime you press a button it ads 25 points to the score board.
At first I wanted the button press to add a random number between 42-57 to the score board.
And then how to do convert that int or long to a string to make it fit into a textview and keep the current score, and then add a new score.
EDIT: ok so someone said I should post the code so here it is.. where do i put this..
TextView txv182 = (TextView) findViewById(R.id.welcome);
txv182.setText(toString(finalScore));
Because when I do it, I get an error: The method toString() in the type Object is not applicable for the arguments (int)
public class MainActivity extends Activity {
// Create the Chartboost object
private Chartboost cb;
MediaPlayer mp = new MediaPlayer();
SoundPool sp;
int counter;
int db1 = 0;
Button bdub1;
TextView txv182;
int finalScore;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv182 = (TextView) findViewById(R.id.welcome);
finalScore = 100;
sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
db1 = sp.load(this, R.raw.snd1, 1);
bdub1 = (Button) findViewById(R.id.b4DUB1);
bdub1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (db1 != 0)
sp.play(db1, 1, 1, 0, 0, 1);
txv182.setText(finalScore);
}
});
First you need to store your score to certain integer variable say score and set it to any initial value you want and use.
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(toString(score));
you do not need to initialize the textview with value just in onclick() of button do score+=25and add text to your textview as above.
hope this helps
It's actually like this..
pernts+=25;
txv182.setText(String.valueOf(pernts));
in android, after the
public class MainActivity extends Activity {
but before..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i wrote..
int pernts;
String strI;
so then after the above #Override majigy, i wrote..
strI = "" + pernts;
pernts = 0;
because when i wrote it like.. int pernts = 0; it never worked, forcing me to add final and what not..
any way.. How to convert an integer value to string? anSwered the question..
and so the end was like this
pernts+=25;
txv182.setText(String.valueOf(pernts));
i figured out you have to add a value to the int variable not the string.. i kept wanting to add 25 and i was getting 25252525252525.., instead of 25 50 75 100.. kinda cool actually.. separating the logic of how to "add" 25 .. anyway thanks.. SOF!
30 minutes later... found this.. How can I generate random number in specific range in Android?
..
import android.app.Activity;
public class InsMenu extends Activity {
static TextView txv182;
static int pernts;
Button bdub1;
public static void addPernts() {
int min = 37;
int max = 77;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
pernts+=i1;
txv182.setText(String.valueOf(pernts));
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.BLAHBLAH);
bdub1 = (Button) findViewById(R.id.b4DUB1);
txv182 = (TextView) findViewById(R.id.welcome);
bdub1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPernts();
}
--- i created my first objekt thanks to this too.. http://www.tutorialspoint.com/java/java_methods.htm ..

Categories