This question already has answers here:
how to resolve java.lang.string cannot be cast to java.lang.integer android with xml parser
(2 answers)
Converting a string to an integer on Android
(14 answers)
Closed 5 years ago.
In the text view, I'll put a variable value of the string as text.
The error I'm facing is:
FATAL EXCEPTION: main Process: app.com.application, PID: 14336 Java. Lang. ClassCastException: Java. Lang. String cannot cast to Java. Lang. Integer atapp.com.application.activity.PayActivity$1.onCheckedChanged(PayActivity.java:41)
Also, this is my code for this part:
public class PayActivity extends AppCompatActivity {
Button button;
RadioGroup radioGroup;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay);
button = findViewById(R.id.btnPay);
radioGroup = findViewById(R.id.radioPay);
textView = findViewById(R.id.pay);
final String[] TextShow ={
"مبلغ این بسته 2500 تومان است",
"مبلغ این بسته 7000 تومان است",
"مبلغ این بسته 10000 تومان است",
"مبلغ این بسته 20000 تومان است"
};
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedId);
final int ArticleTag = (int) radioButton.getTag();
textView.setText(TextShow[ArticleTag]);
Toast.makeText(PayActivity.this,TextShow[ArticleTag] , Toast.LENGTH_SHORT).show();
}
});
}
}
Also, tags include "0" ,"1", ,"2" and "3".
Can you guide me where is my problem?
Thanks
You can't cast a string containing an integer value to an integer value:
(int) radioButton.getTag()
You have to explicitly convert to a string (since getTag() returns an Object), then parse:
Integer.parseInt(radioButton.getTag().toString())
Related
This question already has answers here:
What is an off-by-one error and how do I fix it?
(6 answers)
Closed 1 year ago.
Hello I am new to android java codding I created a "login activity" and "Users class"
and my condition is to add a number of users and pass to the "Users class" and check if the password given is correct then log in,
however, it works correctly when I put the right password but if the password is wrong the app just crashes and the if-else condition does not run.
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Loginmsg;
public Button Btlogin;
public static int Counter;
public static ArrayList <User> mUsers;
public static int Tester;
private String nameHolder;
private String passHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = findViewById(R.id.etpsudo);
Password = findViewById(R.id.etpaswword);
Loginmsg = findViewById(R.id.txtlog);
Btlogin = findViewById(R.id.btnlogin);
Tester=0;
Btlogin.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
nameHolder=Name.getText().toString();
passHolder=Password.getText().toString();
for(int i=0; i<=mUsers.size();i++){
if((nameHolder.equals(mUsers.get(i).getmNom()))&&(passHolder.equals(mUsers.get(i).getmPass()))){
Counter=i;
i=mUsers.size()+1;
Tester=1;
}
}
if (Tester==1){
Intent drawless = new Intent(MainActivity.this,newacc.class);
startActivity(drawless);
}
else{
Loginmsg.setText("eroor");
}
}
});
mUsers = new ArrayList<>();
mUsers.add(new User("amine","12345"));
mUsers.add(new User("bouhali","4523"));
mUsers.add(new User("khawla","ae12"));
}
}
You have a problem in you for loop
Look carefully at
for(int i=0; i<=mUsers.size();i++).
ArrayLists are index starting at 0. This mean that the last element is less than the total number, not equal to the total number.
Also I agree with Dave, use a break rather than modifying the value of i to end the loop, it is much clearer.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have a problem in my code in android studio, I created it to say "Hello" when the person "abc" writes but that didn't work . can u plz help me. here is my codes
final Button butt=(Button)findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText frag =(EditText)findViewById(R.id.frag);
final TextView hello=(TextView)findViewById(R.id.hello);
String verb =frag.getText().toString();
if (verb=="abc"){
hello.setText("Hello");
Because the condition in if block returns false
Use this code instead.
final Button butt = (Button) findViewById(R.id.butt);
butt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final EditText frag = (EditText) findViewById(R.id.frag);
final TextView hello = (TextView) findViewById(R.id.hello);
String verb = frag.getText().toString();
if ("abc".equals(verb)) {
hello.setText("Hello");
}
}
}
When comparing string, always use .equals method
Example:
String str1 = "yourstring1";
String str2 = "yourstring2";
if(str1.equals(str2))//return false
{...}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Causing error when setText a value in edittext. Id Given is Correct ,if i give set Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
Code Given Below here i cant setText Otp Code "otpcode.setText("12345");" in oncreate it works perfectely.
when i give it in the method"recivedSms".it didn't work.
public class Change_Password_Activity extends AppCompatActivity {
EditText user_name,pass_wd;
public EditText otpcode;
private Button btn_submit;
private String username,otp,password;
private ProgressDialog prgDialog;
private Typeface typeface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change__password_);
typeface = GlobalVariables.getTypeface(Change_Password_Activity.this);
prgDialog = new ProgressDialog(this);
// Set Progress Dialog Text
prgDialog.setMessage("Please wait...");
// Set Cancelable as False
prgDialog.setCancelable(false);
otpcode = (EditText)findViewById(R.id.otpedittext);
user_name = (EditText) findViewById(R.id.edittext_ch_user);
pass_wd = (EditText) findViewById(R.id.edittext_ch_passwd);
btn_submit = (Button) findViewById(R.id.button_changepswd);
otpcode.setTypeface(typeface);
user_name.setTypeface(typeface);
pass_wd.setTypeface(typeface);
btn_submit.setTypeface(typeface);
}
public void recivedSms(String message)
{
try
{
int smsnumbr= Integer.parseInt(message);
otpcode.setText(smsnumbr);
}
catch (Exception e)
{
Log.e("error", String.valueOf(e));
}
if you are trying to set smsnumbr to edittext then it will give null pointer exception as in setText(Integer) android tries to find a resource from R.java file with given integer as id. to achieve what you want you should use String.valueOf(..) instead.
int smsnumbr= Integer.parseInt(message);
otpcode.setText(String.valueOf(smsnumb));
This question already has an answer here:
Android discount app is not running [closed]
(1 answer)
Closed 8 years ago.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ayak1=(EditText) findViewById(R.id.editText1);
say1 = Integer.parseInt(ayak1.getText().toString());
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==button1){
int sonuc=(say1)*(6)/(10);
String total = String.valueOf(sonuc);
fiyat.setText(total);
}
this is the code,can you please help?
Your ayak1.getText().toString() String is empty. Integer.parseInt() would give you NumberFormatException if you pass to it an empty String (or any String that can't be parsed as an int).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to nicely format floating types to String?
How can I list the number with up to two decimal places? I tried this method: http://developer.android.com/reference/java/text/NumberFormat.html but no luck. Below code. Maybe someone can help me.
package karcio.fuel.economy;
public class FuelEconomy extends Activity
{
private EditText editText1;
private EditText editText2;
private TextView textView4;
private TextView textView6;
private Button button1;
private double miles;
private double liters;
private double result;
private double convertMilesToKm;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initParams();
}
private void initParams()
{
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
textView4 = (TextView)findViewById(R.id.textView4);
textView6 = (TextView)findViewById(R.id.textView6);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
calculate();
}
});
}
private void calculate()
{
miles = Double.parseDouble(editText1.getText().toString());
liters = Double.parseDouble(editText2.getText().toString());
convertMilesToKm = miles * 1.61;
result = 100 * liters / convertMilesToKm;
textView6.setText(Double.toString(convertMilesToKm));
textView4.setText(Double.toString(result));
}
}
You can do something like this:
String str = String.format("%.2f", 3.99999);
textView.setText(str);
Well you can try to manually do it.
//This is just an example
double number = result; //result is YOUR variable (ex. result = 23.1231231241920312)
int tmp = number * 100; //2312.31231241920312
number = (double)tmp / 100; //23.12
Hope this helps.
Note: You can skip the step where i declare an INT if you do it on the other line.
Update: The advantage of using this method is that you do not need to create an Object which is faster, but of course there are many ways.