setText() error, Method cannot resolve for TextView - java

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. :)

Related

App crashes after .putExtra intent

I'm trying to send a variable from one activity to another. When I try to test the app on my phone, it crashes and says the app has stopped working.
Code from sending side
if (id==R.id.action_cart){
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
}
and code on receiving side
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
If you know what the issue is please let me know. I've spent hours looking for a solution. Thank you
you need to move this TextView btn = (TextView) findViewById(R.id.text); inside oncreate because before oncreate there is no layout attached to Scroll activity and finding textview by using this findViewById(R.id.text) will result in a failure hence crash
public class Scroll extends AppCompatActivity {
TextView btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text)
//^^^^
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(""+intValue);
}
}
Note : Use appropriate naming convention for clarity , like textViewVar for TextView instead of btn and convert your int value to text using ""+value because TextView allow only String to use
I think the problem is in this call: btn.setText(intValue);: string resource ID with value 5 does not exist. Try replacing it with btn.setText(Integer.toString(intValue));
Try this
if (id==R.id.action_cart){
//hide views before moving
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
}
and in Recieving activity
put this in oncreate
btn = (TextView) findViewById(R.id.text);
Like this
public class Scroll extends AppCompatActivity {
TextView btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
change a line to: btn.setText(String.valueOf(intValue));
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
In your activity
public class Scroll extends AppCompatActivity {
TextView btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text); // initialize your button here
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
Replace your code by this
public class Scroll extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
TextView btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}
}

equals() is not working

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.

How is it getting an instantiation error and how to fix it? [Android]

I have been coding a simple android Java program, so far I included an ImageView, button and an EditText in the activity_main.xml. However, when I run it, with this MainActivity java class:
public class MainActivity extends Activity {
private Button start = (Button) findViewById(R.id.start);
private EditText nameField = (EditText) findViewById(R.id.nameField);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = nameField.getText().toString();
}
});
}
is not working and it returns a fatal error I cannot resolve. It says:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo, java.lang.NullPointerException
How can I fix it? Thanks a lot!
You should move these initializations to onCreate :
private Button start = (Button) findViewById(R.id.start);
private EditText nameField = (EditText) findViewById(R.id.nameField);
i.e :
private Button start;
private EditText nameField;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
nameField = (EditText) findViewById(R.id.nameField);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = nameField.getText().toString();
}
});
}
When you call findViewById(R.id.start) in the member initialization expression, the Activity instance is not initialized yet, which causes the NullPointerException.

How to init Objects/Data in Android correctly

i am wondering how can i init Objects in my Activity correctly without getting NPE?
Normally I am initialising my Objects in the onCreate() method but when I do so I can't access them later in for example in an onClick() method.
What is the right way to do it?
Thanks Jérôme
public class MainActivity extends ActionBarActivity {
Button sendButton;
EditText textMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = (Button) findViewById(R.id.button);
EditText textMessage = (EditText) findViewById(R.id.message);
}
public void sendMessage() {
setContentView(R.layout.activity_main);
String actualMessage = textMessage.getText().toString();
System.out.println(actualMessage);
}
Simply remove the types Button and EditText when you initialize them in onCreate. Having the type when you set it equal to a view declares a new variable, instead of referencing the class variables.
sendButton = (Button) findViewById(R.id.button);
textMessage = (EditText) findViewById(R.id.message);
Now you can use this control anywhere in this activity
Button sendButton;
EditText textMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button) findViewById(R.id.button);
textMessage = (EditText) findViewById(R.id.message);
}

Android get text from editText

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button = (Button) findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView3);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
input1 = editTextView1.getText().toString();
input2 = editTextView2.getText().toString();
First initialize the editText just like you initialize the Button and TextView then you are able to get the text from editText
you must have initialize.
(EditText, input1 and input2).
in Activity :
EditText editTextView1;
EditText editTextView2;
String input1;
String input2;
in onCreate :
editTextView1 = (EditText) findViewById(yourEditText1_Id);
editTextView2 = (EditText) findViewById(yourEditText2_Id);
Here is a little code that might help point you in the right direction. Post your code and I'll make the code fit your needs.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
TextView textView = (TextView)findViewById(R.id.textView3);
EditText editText1 = (EditText) findViewById(R.id.editTextView1);
EditText editText2 = (EditText) findViewById(R.id.editTextView2);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
input1 = editText1.getText().toString();
input2 = editText2.getText().toString();
break;
}
}
Just going to make some of the answers above a bit clearer. An Activity is like a Class in a regular Java program. That means that it has fields (variables) and methods (functions). You can't access a field from within a method unless you declare in within the body of the class, like this:
public class MainActivity extends Activity implements OnClickListener{
EditText input1;
protected void onCreate(Bundle savedInstanceState){
// normal onCreate stuff
input1 = (EditText) findViewById(R.id.input1);
// this should be the id in your layout.xml file
}
public String getText(){
String s = input1.getText().toString().trim();
return s;
}
}
Note the use of the trim() method, which will remove any extra whitespace the user may have added at the end of their input.

Categories

Resources