equals() is not working - java

package com.example.life.bluebell;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
System.out.println("asdfg");
}
}
i am getting error at
if(et.equals("ab123"))
'equals' is shown as error with cannot resolve symbol type
i have tried comparing some other predeclared string instead of 'et' but still it is generating same error

public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
}
private void init() {
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}

Write the following inside your OnCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("sss"))
{
System.out.println("asdfg");
}
}

Use the comparison code inside the method scope of onCreate(). In java, it is not allowed to do operations outside a method or an instance block or a static block inside a class.
Modify your own code like:
package com.example.life.bluebell;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
Log.d("Myactivity","asdfg");
//system out will not be of much use and will only print on console. Use log instead to see output in Logcat
}
}
}

Try this , I am sure this will work
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}

try to put this code in some method like
public void getData(){
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
System.out.println("asdfg");
}
}
and dont call this getData() in onCreate(),
call it in some Button click action like follows
cButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getData();
}
});
your complete code would be like
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button cButton = (Button) findViewById(R.id.custom_button);
cButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getData();
}
});
}
public void getData() {
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}

First u have to check the reference editText is correct or not, then after just follow the below codes,
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkValue();
}
public void checkValue()
{
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString().trim();
if(et.contentEquals("ab123")) // if(et.equalsIgnoreCase("ab123"))
{
System.out.println("asdfg");
}
}
}
It will help u.

Related

setText() error, Method cannot resolve for TextView

Method cannot resolve for TextView in this code
What is wrong with this?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText Username = (EditText) findViewById(R.id.EmployerNameEdit);
String userName = Username.getText().toString();
TextView changedName = (TextView) findViewById(R.id.employerName);
changedName.setText(userName);
}
Every thing in oncreate or create global edittext and TextView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Username = (EditText) findViewById(R.id.EmployerNameEdit);
String userName = Username.getText().toString();
TextView changedName = (TextView) findViewById(R.id.employerName);
changedName.setText(userName);
}
might be you missed curly bracket,
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Username = (EditText) findViewById(R.id.EmployerNameEdit);
String userName = Username.getText().toString();
TextView changedName = (TextView) findViewById(R.id.employerName);
changedName.setText(userName);
}
}
if you still getting problem, please fell free to ask question. :)

Button click is not working: button does nothing

I'm using the following class and ive been trying for the last 24 hours i cant figure out why its not working. when i press the button it does nothing.
I'm creating this so i can verify the use login information
My class
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG);
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
});
}}
I Added my XML file on https://codeshare.io/5XWxOE
You are creating Toast but do not call it to show its content try code blew :
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG).show();
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
you forgot to use show()
Please try this solution
Toast.makeText(login.this,"it
clicked",Toast.LENGTH_LONG).show;
Instead of the getApplicationContext() you have.
Try this ...
TextView mt;
Button clickButton;
mt=(TextView) findViewById(R.id.messy);
clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Button click toast", Toast.LENGTH_SHORT).show();
mt.setText("Your text here");
}
});
When something is going wierd.. you can check basic log. try this one~
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setText("Button Found");
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
clickButton.setText("Button Clicked");
}
});
}}
There is another way to write code for button clicking
public class login extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button firstButton = (Button) findViewById(R.id.loginButton);
firstButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginButton: {
Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.messy);
textView.setText("Your text here");
break;
}
default: {
Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
I hope this will resolve your issue.

Want to display stored name/value data in intent to new activity screen

I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation. What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class. Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):
MainActivity from which I want to store data to send to ThirdActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a2_button = (Button) findViewById(R.id.a2_button);
a2_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a2intent = new Intent("com.tester.lab.x");
startActivityForResult(a2intent, 1);
}
});
Button a3_button = (Button) findViewById(R.id.a3_button);
a3_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);
a3intent.putExtra("greeting","my Name");
startActivity(a3intent);
}
});
}
}
ThirdActivity:
public class ThirdActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
getIntent().getStringExtra("greeting");
}
}
How would I display the string "my Name" once the user clicks ThirdActivity button? Any help is wonderful, thanks!
Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
Or by using Button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
Button three = (Button) findViewById(R.id.your_button_id);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
});
}

How to debug an Android app force close error?

public class MainActivity extends Activity {
EditText oETextN;
EditText oETextPh;
Button oButtonSave;
public String givenName;
public String givenPhNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewsById();
oButtonSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
givenName = oETextN.getText().toString();
givenPhNum = oETextPh.getText().toString();
OrgiOperations.Insert2Contacts(getApplicationContext(),
givenName, givenPhNum);
if (OrgiOperations.isTheNumberExistsinContacts(
getApplicationContext(), givenPhNum)) {
Log.i(OrgiOperations.TAG, "Exists");
} else {
Log.i(OrgiOperations.TAG, "Not Exists");
}
}
});
setContentView(R.layout.activity_main);
}
private void findViewsById(){
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
//oEText = (EditText) findViewById(R.id.Name);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I've been trying to make a simple Android app that saves the entered details (Name and Phone Number) directly to your device phonebook. This is the MAIN activity, the other class includes the functions to do this, which I'm sure are implemented correctly.
The problem rises when I run the application on my phone, and unluckily after hours of cold coding I get the FORCE CLOSE error!
Change to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
Then
private void initializeViews(){
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
//oEText = (EditText) findViewById(R.id.Name);
}
You need to set the content of the layout first to the activity then initialize views.
Also rename findViewsById(); to initializeViews() or something more appropriate coz you initialize your views with findViewById as oButtonSave = (Button) findViewById(R.id.SaveBtn);
Edit:
Have the click listener after initialization of view.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
oButtonSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
givenName = oETextN.getText().toString();
givenPhNum = oETextPh.getText().toString();
OrgiOperations.Insert2Contacts(getApplicationContext(),
givenName, givenPhNum);
if (OrgiOperations.isTheNumberExistsinContacts(
getApplicationContext(), givenPhNum)) {
Log.i(OrgiOperations.TAG, "Exists");
} else {
Log.i(OrgiOperations.TAG, "Not Exists");
}
}
});
}

android - java.lang.RuntimeException

Why am I getting an java.lang.runtimeexception: Unable to instantiate activityComponentInfo {com.example.carprofile/com.example.carprofile.CarRegistraion}: java.lang.NullPointerExceprion
This is my code:
public class CarRegistration extends Activity{
//ParseXML parseClass;
Button saveCar;
EditText carMake = (EditText) findViewById(R.id.etCarMake);
EditText carModel = (EditText) findViewById(R.id.etCarModel);
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}
if I clear the onCreate method, from saveCar = (Button)..., everything works fine.
Can someone tell me what am I doing wrong?
Try this,
public class CarRegistration extends Activity{
Button saveCar;
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
EditText carMake = (EditText) findViewById(R.id.etCarMake); //these lines should be after setContentView()
EditText carModel = (EditText) findViewById(R.id.etCarModel);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}

Categories

Resources