The application is crashing when pressing the Computecost button
package com.example.hw_3;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class ShoppingExpensesPage extends Activity
{
TextView et;
Button computecost;
Button save;
Button cancel;
RadioGroup drinks;
RadioButton drink;
int tottalcost=0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
et=(TextView)findViewById(R.id.tv11);
Bundle extra = getIntent().getExtras();
String val1 = extra.getString("value");
et.setText(val1);
computecost=(Button)findViewById(R.id.btn11);
computecost.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
String val;
int selectedId = drinks.getCheckedRadioButtonId();
drink = (RadioButton)findViewById(selectedId);
val=(String) drink.getText();
if(val=="Juice")
{tottalcost=tottalcost+3;
}
else if (val=="Cola")
{
tottalcost=tottalcost+2;
}
et.setText(Integer.toString(tottalcost));
}
});
save=(Button)findViewById(R.id.btn21);
save.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
String str=Integer.toString(tottalcost);//(String) et.getText();
returnIntent.putExtra("return", str);
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
}
You haven't set the view for drinks.
int selectedId = drinks.getCheckedRadioButtonId();
Find a view for it, before computecost.setOnClickListener:
drinks = (RadioGroup) findViewById(...);
This is the wrong way to compare Strings
if(val=="Juice")
In Java, "==" compares the reference of the Objects but not their values. You need to use .equals()
if("Juice".equals(val))
{ // do something }
If this doesn't solve your problem then please post the logcat from the error but this still needs to be changed.
Related
i tried to make my first converter in android studio with java but 2 functions setText and getText are not working
package com.example.unitconvertor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity<editTextTextPersonName> extends AppCompatActivity {
private Button button;
private editTextTextPersonName editTextTextPersonName;
private View editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
int inch = Integer.parseInt(s);
double cm = 2.54 * inch;
editText.setText( "the value is" + cm);
}
});
}
}
i hope that i am clear
thankyou
Try this-
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.your_id);//ADD YOUR EDIT TEXT ID HERE
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "value saved", Toast.LENGTH_SHORT).show();
String s = editText.getText().toString();
int inch = Integer.parseInt(s);
double cm = 2.54 * inch;
editText.setText( "the value is" + cm);
}
});
}
}
The error is that your "edittext" is declared as view, correct it like this : private EditText edittext;
then, retrieve it by id like this in your onCreate method edittext = findViewById(R.id.your_id);
I am developing an login and register app. The app is supposed to check if the entered data is correct or not while login or register. The login validation works fine but register doesn't validate data. The register button is supposed to check validations after clicking but nothing happens when I click it. There is no error shown in android studio and the app runs fine.
package com.example.investas;
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 android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
}
});
}
private void validations() {
String username=inputUsername.getText().toString();
String email=inputEmail.getText().toString();
String password=inputPassword.getText().toString();
String cpassword=inputCpassword.getText().toString();
if(username.isEmpty() || username.length()<7)
{
showError(inputUsername,"Your username is not valid");
}
else if(email.isEmpty() || !email.contains("#"))
{
showError(inputEmail,"Email is not valid");
}
else if(password.isEmpty() || password.length()<7)
{
showError(inputPassword,"Password must be 7 characters");
}
else if(cpassword.isEmpty() || !cpassword.equals(password))
{
showError(inputCpassword,"Passwords are not matching");
}
else
{
Toast.makeText(getApplicationContext(),"Registration
Successful",Toast.LENGTH_SHORT).show();
}
}
private void showError(EditText input, String your_username_is_not_valid) {
input.setError(your_username_is_not_valid);
input.requestFocus();
}
}
Try to move the following lines out of the OnClickListener, they should be in the onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//...
inputUsername = findViewById(R.id.inputUsername);
inputEmail = findViewById(R.id.inputEmail);
inputPassword = findViewById(R.id.inputPassword);
inputCpassword = findViewById(R.id.inputCpassword);
btnRegister = findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
// ...
}
Doing it this way is necessary for it to be cleaner and to work.
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validations();
}
});
}
I have an android studio assignment that requires us to create 3 activities, one of these activities has a spinner and text and we must pass the text and selected spinner value from the activity to another activity to show the input I figured out how to pass the text but I can't quite figure how to do the same to the spinner. this is the spinner code.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Homepage<adapter> extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Button btn;
EditText et;
String st;
String selectedItem="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
btn = findViewById(R.id.Btnstart);
et = findViewById(R.id.Std_Name);
Spinner spinner = findViewById(R.id.major_spinner);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Homepage.this, Input.class);
st = et.getText().toString();
i.putExtra("Value", st);
startActivity(i);
finish();
}
});
/**
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Homepage.this, Input.class);
selectedItem = spinner.getSelectedItem().toString();
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
}
});
**/
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.majors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Move your text variable to the scope of the activity and pass in the Intent, like you did with your st variable.
I made a basic homepage for my app.
The page consists of a sign in and sign up button.
Only the signup button works. I'm fairly new to android development and I guess I just can see why this is happening.
I checked other questions on stack overflow and decided to make a private handler and then cases however I still have the same problem.
Here is the code. Any help and insight as to why this is happening is much appreciated.
package net.citrusdynamics.citrus;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
signin.setOnClickListener(onclicklistener);
signup.setOnClickListener(onclicklistener);
}
private View.OnClickListener onclicklistener = new View.OnClickListener() {
#Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.signin:
setContentView(R.layout.activity_signin);
Intent signin_intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(signin_intent);
break;
case R.id.signup:
setContentView(R.layout.activity_signup);
Intent signup_intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signup_intent);
break;
}
}
};
}
Try to add the click listener like in this code below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
signin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent signin_intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(signin_intent);
}
});
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent signup_intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signup_intent);
}
});
}
I am writing a method, that will read a string, split a string, and then display one of the values in a text field. Here is what I have so far, it is compiling, but when the button is clicked, nothing is populated into the text field. Thanks
import java.io.DataInputStream;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class AboutScreen extends Activity{
Button homeButton=null;
Button getReactant1=null;
TextView tv=null;
String is=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
homeButton=(Button)findViewById(R.id.Home);
homeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
getReactant1=(Button)findViewById(R.id.combinationinterface);
getReactant1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String string1 = getResources().getString(R.string.MgandO);
String delims =",";
String[] tokens = string1.split(delims);
String i = null;
tokens[0]= i;
String f = null;
tokens[1]= f;
tv=(TextView)findViewById(R.id.R1TextInput);
is=i;
tv.setText(i);
}
});
}
});}
Because you called the finish() method at the beginning of the onClick(). Which stops your activity without doing the following stuff.
Your first problem is setting OnClickListener of getReactant1 button inside of homeButton's OnClickListener.
Your other problem is using irrelevant variables and not using splitted value at your textView.
Try to use and modify code below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
homeButton=(Button)findViewById(R.id.Home);
homeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
getReactant1=(Button)findViewById(R.id.combinationinterface);
getReactant1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String string1 = getResources().getString(R.string.MgandO);
String delims =",";
String[] tokens = string1.split(delims);
String is=token[0];
// or set it to another token you want i.e. token[1]
tv=(TextView)findViewById(R.id.R1TextInput);
tv.setText(is);
}
});
}