I am trying to work with two buttons on one screen and I want to have two different actions occur when each button is clicked. This is my code and I am getting the error message onClick view is already defined on the method titles of both onClick methods. Any help is greatly appreciated.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private Button button3;
private Button button2;
private EditText editText;
private EditText editText9;
private EditText editText10;
private EditText editText11;
private EditText editText12;
private EditText editText13;
private EditText editText14;
private TextView textView;
private TextView textView22;
private View view;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent intent = getIntent();
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
editText = (EditText) findViewById(R.id.editText);
editText9 = (EditText) findViewById(R.id.editText9);
editText10 = (EditText) findViewById(R.id.editText10);
editText11 = (EditText) findViewById(R.id.editText11);
editText12 = (EditText) findViewById(R.id.editText12);
editText13 = (EditText) findViewById(R.id.editText13);
editText14 = (EditText) findViewById(R.id.editText14);
textView = (TextView) findViewById(R.id.textView);
textView22 = (TextView) findViewById(R.id.textView22);
}
public void onClick (View view) {
this.view = view;
if (view.getId() == R.id.button3) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
MainActivity2.this.startActivity(intent);
}
}
public void onClick (View view){
if (view.getId() == R.id.button2) {
String value8 = editText.getText().toString();
String value9 = editText9.getText().toString();
String value10 = editText10.getText().toString();
String value11 = editText11.getText().toString();
String value12 = editText12.getText().toString();
String value13 = editText13.getText().toString();
String value14 = editText14.getText().toString();
}
}
}
You have two onClick methods with the same parameters so it doesn't know which to use. Combine the two and it should work.
Related
package com.example.addarray;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
int mCounter = 0;
TextView textView;
TextView textView2;
TextView textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(view -> {
mCounter++;
textView = findViewById(R.id.textView1);
textView.setText(Integer.toString(mCounter));
textView2 = findViewById(R.id.textView2);
editText = findViewById(R.id.editText);
String [] string = new String[mCounter];
for(int i =mCounter-1; i>mCounter; i++){
string[i] = editText.getText().toString();
}
textView2.setText(Arrays.toString(string));
textView3 = findViewById(R.id.textView3);
textView3.setText(editText.getText().toString());
});
}
}
I would like to add the input from the edit text into an array upon each click of the button, however, the element of the array only displayed null as shown in the picture. Is there any way to fix it? Thank you.
You create a new array with each click. No need to create a new array on every click. It is possible to save new values in ArrayList.
public class MainActivity extends AppCompatActivity {
private ArrayList<String> list = new ArrayList<String>();
// *******
button.setOnClickListener(view -> {
// *******
list.add(editText.getText().toString());
// ******
}
}
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);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Hi i am doing a project that is due wednesday and i have spent over 13 hours trying to get Android studio working again, first their was a problem with the gradle and now i keep getting a nullpointerexception even though it same code that was working before. here is the code, please help? because i am stuck and could be doing my project rather that trying to solve android studios problems. by the way there are no errors within the code it says but here is the code.
package ie.wit.fitnessmadeeasy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LogActivity extends AppCompatActivity {
DataBaseHelper helper = new DataBaseHelper(this);
EditText uname = (EditText) findViewById(R.id.et_username);
String unstr = uname.getText().toString();
EditText pass = (EditText) findViewById(R.id.et_password);
String passstr = pass.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
TextView registerLink = (TextView) findViewById(R.id.regHere); //register link creates a link between the two pages
registerLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(LogActivity.this, RegisterActivity.class);
LogActivity.this.startActivity(registerIntent); //what this does it creates an intent that opens the registeravtivity then it tells the current activity to perform the intent and open the register page
}
});
}
// Button login = (Button) findViewById(R.id.login);
public void onLogClick(View view) {
if (view.getId() == R.id.login) {
String Password = helper.searchPassstr(unstr);
Log.v("pass", passstr);
Log.v("pass", Password);
if (passstr.equals(Password)) {
Intent i = new Intent(LogActivity.this, UserAreaActivity.class);
i.putExtra("username", unstr);
startActivity(i);
} else {
Toast temp = Toast.makeText(LogActivity.this, "Username and Password don't Match", Toast.LENGTH_SHORT);
temp.show();
}
}
}
}
package ie.wit.fitnessmadeeasy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
//import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
DataBaseHelper helper = new DataBaseHelper(this);
final EditText name = (EditText) findViewById(R.id.et_name);
final EditText username = (EditText) findViewById(R.id.et_username);
final EditText password1 = (EditText) findViewById(R.id.et_password);
final EditText password2 = (EditText) findViewById(R.id.et_password);
String namestr = name.getText().toString();
String usernamestr = username.getText().toString();
String password1str = password1.getText().toString();
String password2str = password2.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// final EditText password2 = (EditText) findViewById(R.id.et_password);
name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(name.getText().length()==0){
name.setError("Fill Out");
}
else if(username.getText().length()==0){
username.setError("Fill Out");
}
}
});
}
public void onRegClick(View v)
{
if(v.getId() == R.id.confirm)
{
// EditText age = (EditText) findViewById(R.id.et_age); //age is a final variable and is only assigned to activity_register as a view
// EditText name = (EditText) findViewById(R.id.et_name);
// EditText username = (EditText) findViewById(R.id.et_username);
// EditText password1 = (EditText) findViewById(R.id.et_password);
// EditText password2 = (EditText) findViewById(R.id.et_password2);
if(namestr.equals("")){
Toast pass = Toast.makeText(RegisterActivity.this, "Enter Name!", Toast.LENGTH_SHORT);
pass.show();
}
if(usernamestr.equals("")){
Toast pass = Toast.makeText(RegisterActivity.this, "Enter Username!", Toast.LENGTH_SHORT);
pass.show();
}
if(password1str.equals("")){
Toast pass = Toast.makeText(RegisterActivity.this, "Enter Password!", Toast.LENGTH_SHORT);
pass.show();
}
if(!password1str.equals(password2str))
{
//popup message
Toast pass = Toast.makeText(RegisterActivity.this, "Passwords don't match!", Toast.LENGTH_SHORT);
pass.show();
}
else{
// String namestr = name.getText().toString();
//insert details
RegRequest reg = new RegRequest();
reg.setEt_name(namestr);
reg.setEt_username(usernamestr);
reg.setEt_password(password1str);
// r.setEt_age(agestr);
Toast success = Toast.makeText(RegisterActivity.this, "Success", Toast.LENGTH_SHORT);
success.show();
Intent i = new Intent(RegisterActivity.this, LogActivity.class);
startActivity(i);
helper.insertUser(reg);
}
}
}
}
package ie.wit.fitnessmadeeasy;
public class RegRequest{
String et_name , et_username, et_password;
// int et_age;
public void setEt_name(String et_name)
{
this.et_name = et_name;
}
public String getEt_name()
{
return this.et_name;
}
public void setEt_username(String et_username)
{
this.et_username = et_username;
}
public String getEt_username()
{
return this.et_username;
}
public void setEt_password(String et_password)
{
this.et_password = et_password;
}
public String getEt_password()
{
return this.et_password;
}
}
at ie.wit.fitnessmadeeasy.LogActivity.<init>(LogActivity.java:17)
LogActivity.java
Only declare the fields like the following:
EditText uname, pass;
String unstr, passstr;
Initialize in onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
uname = (EditText) findViewById(R.id.et_username);
unstr = uname.getText().toString();
pass = (EditText) findViewById(R.id.et_password);
passstr = pass.getText().toString();
}
And RegisterActivity.java
DataBaseHelper helper ;
EditText name, username,password1,password2;
String namestr,usernamestr,password1str,password2str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
helper = new DataBaseHelper(this);
name = (EditText) findViewById(R.id.et_name);
username = (EditText) findViewById(R.id.et_username);
password1 = (EditText) findViewById(R.id.et_password);
password2 = (EditText) findViewById(R.id.et_password);
namestr = name.getText().toString();
usernamestr = username.getText().toString();
password1str = password1.getText().toString();
password2str = password2.getText().toString();
}
There probably is a really simple answer to this. My goal is to divide the the inputed information x and y and put that into z.
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public int x = Integer.parseInt("");
public int y = Integer.parseInt("");
public int z = (y/x);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button
Button btn = (Button) findViewById(R.id.button);
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);
x = Integer.parseInt(nop);
y = Integer.parseInt(cob);
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(z);
}
});
}
}
Declare z inside your onclick function. Or assign it a value inside your function.
Also nop and cob are editText's get the data in it, use getText().
Because Integer.parseInt() accepts only a String but not an editText, so you must use getText() to get the String value in it.
x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int z = x / y;
tv.setText(z);
}
});
Also z is declared globally and assigned nothing. But it needs to be assigned a value when ever you click the button, so that your editText changes every time you click the button.
Convert first as a string from Edittext then make it as Integer.
String Val1 = nop.getText().toString();
String Val2 =cob.getText().toString();
x = Integer.parseInt(val1);
y = Integer.parseInt(val2);
I'm using onClick() method by implementing OnClickListener class. Using below code but it does not work! There are no errors but it doesn't work. When I click on the button nothing happens. What is wrong?
package com.behnam.PhoneCh.main;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id. Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
}
You never set your listener on your Buttons
mainBtn.setOnClickListener(this);
You will need to add this for all of the Buttons that you want to use the onClick().
See this answer for different ways of handling onClick()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2.setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id.button1;
Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
Change your onCreate and onClick with the above code.
You need to change your code to :
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2 .setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
// And then you can use
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Dome some stuff on click of button 1
break;
case R.id.button2:
// Dome some stuff on click of button 2
break;
}
}
}
Try this instead:
Button btMain = (Button)findViewById(R.id.button1);
btMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doStuff();
}
});
This is the best way to start understanding how the listeners work.