android application crash after launch - java

public class Game extends AppCompatActivity {
static int i = 0;
static int p = 0;
String porc0;
TextView tvporc;
Button btn;
TextView tvresult;
String mutqbar;
char generacvac;
char mutq;
int mutqitiv;
int generacvacitiv;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Random random = new Random();
btn = (Button) findViewById(R.id.button4);
tvporc=(TextView) findViewById(R.id.tvPorc);
tvresult=(TextView) findViewById(R.id.result);
generacvac=(char)(random.nextInt(26)+'a');
tvresult.setText(generacvac);
generacvacitiv = (int) generacvac;
et=(EditText) findViewById(R.id.editText2);
tvresult.setText(generacvac);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mutqbar = et.getText().toString();
try {
mutq = mutqbar.charAt(0);
mutqitiv = (int) mutq;
if (mutqitiv<97 || mutqitiv>122){
tvporc.setText("Մուտքագրեք տառ");
}
int abs = Math.abs(mutqitiv-generacvacitiv);
if (abs>0 && abs<5) tvresult.setText("Դու շատ մոտ ես!");
else if (abs>=5 && abs<10) tvresult.setText("Դու մոտ ես~");
else if(abs>=10) tvresult.setText("Դու հեռու ես~");
} catch (Exception e1) {
tvresult.setText("Դաշտը դատարկ է");
}
}
});
}}
I have this code, but when I run it, it returns error java.lang.RuntimeException: Unable to start activity ComponentInfo{com.guessit.guessit/com.guessit.guessit.Game}: android.content.res.Resources$NotFoundException: String resource ID #0x66. and crush on device.Already 1 hour I'm trying to find the problem. please help me

Thanks I already find the solution. I writed tvresult.setText(generacvac); but it cast to int, and I need to write tvresult.setText(generacvac+"")

Related

Java/Android : First tiny app answer always "false"

I try to create my first tiny app but i have a problem.
My tiny math app always say my answer is false.I don't understand why.
This is my first app, i don't know where i'm wrong.
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
Answer always "False"
I'm a new student in the dev world.
The problem with code is that you are comparing string with integer, that's why it always returns false as java is strictly typed language.
problem code:
if(str.equals(result)){...}
possible solutions:
if( str.equals(""+result)){...}
or
str.equals(String.valueof(result)) // best solution
or
if(result==Integer.parseInt(str)){...}
Here is corrected code:
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(""+result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
As azurefrog comment says, you're comparing a String to an int. You will need to transform str to an int or result to a String. You can for example do:
String str=question.getText().toString();
if (str.equals(String.valueOf(result))) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}

Spinner value is not captured

Please, do not mark this as duplicate if you are not sure
I have three spinners and a botton. When the botton is clicked, the program makes a calculation depending on the value of the three spinners. Then this value passes two another activity and it shows in an editText. Here is my code:
Main
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
capturarTexto();
}
private void capturarTexto() {
Button button_calc = (Button) findViewById(R.id.button_calc);
button_calc.setOnClickListener(get_edit_view_button_listener);
}
private Button.OnClickListener get_edit_view_button_listener = new Button.OnClickListener() {
public void onClick(View v) {
EditText edit_text = (EditText) findViewById(R.id.textBox1);
String edit_text_value = edit_text.getText().toString();
StringTokenizer st = new StringTokenizer(edit_text_value);
int num_words = st.countTokens();
Spinner espec = (Spinner) findViewById(R.id.espec);
String espec_value = espec.getSelectedItem().toString();
Spinner lengor = (Spinner) findViewById(R.id.lista_origen);
String lengor_value = lengor.getSelectedItem().toString();
Spinner lengdest = (Spinner) findViewById(R.id.lista_destino);
String lengdest_value = lengdest.getSelectedItem().toString();
double precio = 0;
if(espec_value .equals("Medicina")){
if (lengor_value .equals("ES") && lengdest_value .equals("EN")){
precio = num_words * 0.12;
}
if (lengor_value .equals("ES") && lengdest_value .equals("FR")){
precio = num_words * 0.12;
}
if (lengor_value .equals("ES") && lengdest_value .equals("DE")){
precio = num_words * 0.12;
}
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
intent.putExtra("precio",precio);
startActivity(intent);
}
};
}
Main2
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
int precio =(int) intent.getExtras().getInt("precio");
TextView txtCambio = (TextView) findViewById(R.id.textView4);
txtCambio.setText("Precio Total: "+ precio + " €");
}
After testing it, the value passed in this line of code:
intent.putExtra("precio",precio)
is allways 0. But if I change it to this:
intent.putExtra("precio",num_words)
it passes correctly the total number of words. This makes me think that the script is not entering in the first if(espec_value .equals("Medicina")) and then, it is not making any calculation.
Does anyone have an idea of how to solve this problem?
Thank you for your time
You are sending Double value and accessing Integer value.
Change the line in Main3Activity.
double precio = intent.getExtras().getDouble("precio");
If you want to parse double value to int then add one more line
int p = (int) precio;

How to set default value to 0 in edittext when nothing is inputted?

I have an app here which adds the number. I have 4 edittexts here. What I want to happen is that when i entered none in one of the edittexts, it will assume that I entered 0. How can it be done? Here is my code:
public class Order extends Activity {
Button GoBackHome;
private Button button1;
private EditText txtbox1,txtbox2,txtbox3,txtbox4;
private TextView tv;
Button PayNow;
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
GoBackHome = (Button) findViewById(R.id.gohomebutton);
PayNow = (Button) findViewById(R.id.button2);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
txtbox3= (EditText) findViewById(R.id.editText3);
txtbox4= (EditText) findViewById(R.id.editText4);
button1.setOnClickListener(new clicker());
GoBackHome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Intent i = new Intent(Order.this, MainActivity.class);
startActivity(i);
}
});
PayNow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Intent i = new Intent(Order.this, Payment.class);
startActivity(i);
}
});
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b,c,d;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
c = txtbox3.getText().toString();
d = txtbox4.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5;
tv.setText(vis.toString());
}
}
}
You can do as Tushar said or you can initialize the value in the XML. Something like
<EditText
android:name="#+id/editText1"
android:text="0"/>
FYI you might also find it cleaner to handle your button clicks on xml like:
<Button
android:name="#+id/btn1"
android:onClick="handleClicks"/>
and then in java you'd have a public void method:
public void handleClicks(View clickedView){
if(clickedView.getId() == btn1.getId(){
...
} else if (...){}
}
initialize as :
txtbox1.setText("0");
Check the EditText length when you get it
String value = null;
if(ed.getText().length()){
value = textBox.getText().toString();
} else
value = 0+"";
You can set android:hint="0" in your XML file, then, in your code, you can check if it's empty (maybe using TextUtils.isEmpty()) and setting some variable to 0.
android:hint="0" will make a "0" appear in your EditTexts, but the "0" will disappear when anything is inputted.
Then you can change the onClick() to this:
class clicker implements Button.OnClickListener {
public void onClick(View v) {
String a,b,c,d;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
c = txtbox3.getText().toString();
d = txtbox4.getText().toString();
try {
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5;
tv.setText(vis.toString());
} catch (NumberFormatException e) {
vis = "0";
}
// Do something with "vis"
}
}
Or you can create a method to check a value, try to parse to an int or return a default value.
public int getInt(String edtValue, int defaultValue) {
int value = defaultValue;
if (edtValue != null) {
try {
value = Integer.parseInt(edtValue);
} catch (NumberFormatException e) {
value = defaultValue;
}
}
return value;
}
Then you change your call to
vis = this.getInt(a, 0) * 2 + this.getInt(b, 0) * 3 + this.getInt(c, 0) * 4 + this.getInt(d, 0) * 5;

Program crashes with empty text field

I need help trying to fix my code. I thought it was simple (probably is) but I can't get it. I am have a simple adding calculator. It works fine, but if I leave 1 or both Number text fields empty, the program crashes.
I have my if statement, but apparently I am not telling it to do the right thing.
public class MainActivity extends Activity {
Double firstNum, secondNum, answerNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText first = (EditText) findViewById(R.id.txtFirst);
final EditText second = (EditText) findViewById(R.id.txtSecond);
final TextView answer = (TextView) findViewById(R.id.txtAnswer);
Button calc = (Button) findViewById(R.id.btnCalc);
calc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// convert pulled info to double using variable names
firstNum = Double.parseDouble(first.getText().toString());
secondNum = Double.parseDouble(second.getText().toString());
if (first == null || second == null)
{
Toast.makeText(MainActivity.this, "Please Enter value", Toast.LENGTH_SHORT).show();
}
else {
// add numbers
answerNum = (firstNum + secondNum);
//set format
DecimalFormat total = new DecimalFormat ("###,###,###.##");
answer.setText("Answer is " + total.format(answerNum));
}
}
});
}
}
You have exception because of line: Double.parseDouble(second.getText().toString()), it cannot parse empty string to double, so you should add some validation code.
If you want to validate edittext means try this following code it will work fine.
public class MainActivity extends Activity {
double firstNum = 0;
double secondNum = 0, answerNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText first = (EditText) findViewById(R.id.editText1);
final EditText second = (EditText) findViewById(R.id.editText2);
final TextView answer = (TextView) findViewById(R.id.textView1);
Button calc = (Button) findViewById(R.id.button1);
calc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (first.getText().toString().equals("")
|| second.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, "Please Enter value",
Toast.LENGTH_SHORT).show();
} else {
firstNum = Double.parseDouble(first.getText().toString());
secondNum = Double.parseDouble(second.getText().toString());
answerNum = (firstNum + secondNum);
DecimalFormat total = new DecimalFormat("###,###,###.##");
answer.setText("Answer is " + total.format(answerNum));
}
}
});
}
}

Can't to sum array elements?

I'm very new in Android programming, and i'm trying to sum all char elements from datepicker, but with no success.
here is code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = (TextView)findViewById(R.id.textView1);
DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
final int day = dp.getDayOfMonth();
final int month = dp.getMonth();
final int year = dp.getYear();
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
String sdate = String.valueOf(month) + String.valueOf(day) + String.valueOf(year);
char[] c = sdate.toCharArray();
for(int i=0; i<c.length;i++)
{
num+=Integer.valueOf(c[i]);
}
tv.setText(String.valueOf(num));
}
});
}
and output is: 355.
Real sum must to be 20, i want to sum like this 17/06/2013,
so example 1+7+0+6+2+0+1+3 = 20
use Character.getNumericValue
for(int i=0; i<c.length;i++) {
num+=Character.getNumericValue(c[i]);
}
This will work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = (TextView)findViewById(R.id.textView1);
DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
final int day = dp.getDayOfMonth();
final int month = dp.getMonth();
final int year = dp.getYear();
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
String sdate = String.valueOf(month) + String.valueOf(day) + String.valueOf(year);
int[] ee=new int[sdate.length()];
String[] dd=new String[sdate.length()];
for(int i=0;i<sdate.length();i++){
dd[i]=sdate.substring(i,i+1);
ee[i]=Integer.parseInt(dd[i]);
num+=ee[i];
}
tv.setText(String.valueOf(num));
}
});
}
Change from Integer.valueOf() to Character.valueOf() Integer.valueOf(int) takes integer, your char is being cast to int and gives ASCII value.

Categories