EditText edituname = (EditText) findViewById(R.id.username);
EditText editpassword = (EditText) findViewById(R.id.password);
String s1 = edituname.getText().toString();
String s2 = editpassword.getText().toString();
Toast.makeText(Main2Activity.this, s1, Toast.LENGTH_LONG).show();
The string s1 shows null value since the code is inside onCreate method. I want it in onCreate and not on buttonclick. So please tell me a solution to get the value of EditText in onCreate.
The value of edittext will be null at the time of initialization thats why it is null. If you want the value without any button click you can set TextWatcher on edittext you want
Related
I have tried to convert ab EditText value to string, but I don't get the expected result: it shows null...
EditText editsignUpUserName = (EditText)findViewById(R.id.editSignUpUserName);
String uName = editsignUpUserName.getText().toString();
I cleared it,the mistake i have made was converted the edittext to string before even getting the input from user...Complete code given below:
EditText editsignUpUserName = (EditText) findViewById(R.id.editUserName);
signup = (Button) findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s= editsignUpUserName.getText().toString();
}
I want to setText in EditView but it will error. I dont know why.
This is on OnCreate function.
name = (EditText) findViewById(R.id.input_name);
String editname = String.valueOf(getName());
MessageTo.message(this,editname);
name.setText(editname);
The string i will set in EditText name.
public String getName(){
int id = 1;
String data=dbhandler.getName(id);
return data;
}
If i remove this code from the OnCreate function
name.setText(editname);
It will have no error. The MessageTo.message code is where it pops a dialog of the String editname. It will show the String editname.
So i put back the code and change it into this code,
name.setText("testing");
But it still won't work.
I dont know why though. All other sources have EditText.setText("String") as how to setText in EditText but it won't work in my case.
LOGCAT ERROR:
09-20 21:00:36.677 1436-1436/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.organizer/com.organizer.ManageProfileActivity}: java.lang.NullPointerException
Your EditText name appears to be null. In your onCreate method, make sure you are calling setContentView(R.layout.YOUR_LAYOUT) where the layout parameter is an XML layout containing your EditText with the ID R.id.input_name.
You assign it later on with
name = (EditText) findViewById(R.id.input_name);
but if the layout does not contain this ID, name will be null, thus causing your error.
It is correct that you should be using the setText() method on the EditText.
Everything looks correct in terms of your codes, so I would suggest to check for the following things:
Are you calling setText anywhere else?
Is the id of your EditText correct (Is the id of your EditText input_name)
Are you sure you are looking at the right activity.
Are you initializing your name variable in the right scope? (Outside the onCreate Method)
Declare the EditText in the xml file
Find the EditText in the activity
Set the text in the EditText
And If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType. For example:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE);
Or
Use +, the string concatenation operator:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);
or
String.valueOf(int):
ed.setText(String.valueOf(x));
public class MainActivity extends Activity {
EditText number1, number2;
String number_1, number_2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
After running the application, why does number_1 have an empty value.
onCreate method is create view.
So EditText number1, nubmer2 is not create yet.
If you want get number1 or number2's value, try in onPause().
Try this..
You have to get the text in like OnClick while starting application both EditText is empty. So that it returns empty.
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
Make sure you have predefined the value in the edittext and that should work
i need to see your xml.... if your trying to get numbers you have to parse the string to an int anyway... what are you trying to achieve? you simply want the text, from each of the textviews? is there text set in the xml?
Make sure you have predefined the value in the edittext in xml file.
like in your layout file android:text="your string" or else you have to set string after binding
oncreate method like.
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
number1.setText("your string");
then you have to get string from below code.
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
You need to fill the EditTexts with something before you can get their value. So create a button, and after filling the EditTexts, then get their values by clicking the button.
Perhaps like this:
public class MainActivity extends Activity {
EditText number1, number2;
String number_1, number_2;
Button submit;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
submit = (Button)findViewById(R.id.btn1); //Define a button in your layout xml file with the ID field set as "btn1"
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
}
});
I have entered values in my text field, But the debugger shows null value.
The text id's are correct yet no value is being printed.
The log cat is showing null pointer exception. I have declared all the variables in the class.
The edit text string functions are declared in the method itself.
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
et3 = (EditText)findViewById(R.id.editText3);
et4 = (EditText)findViewById(R.id.editText4);
et5 = (EditText)findViewById(R.id.editText5);
set1 = et1.getText().toString();
set2 = et2.getText().toString();
set3 = et3.getText().toString();
set4 = et4.getText().toString();
set5 = et5.getText().toString();
System.out.println("set1 ="+set1);
System.out.println("set2 ="+set2);
All values given input are showing null and these are the few values that are being declared in the xml.
I need to know what mistake i am doing and how to rectify it.
i have initialized the values after setContent the data access happens in button on click listener... i didnt want to post the entire code.. –
I think your problem is same as this question.
You are directly accesssing the EditText's value after initializtion of EditText. You should accept the value on some event like onClick.
Let me Explain,
public class MainActivity extends Activity
{
EditText editText;
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById ( R.id.firstEditText );
String str = editText().getText().toString();
// Here Str will be null blank all the time because EditText it self is blank.
}
}
Now see this
public class MainActivity extends Activity implements OnClickListener
{
EditText editText;
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById ( R.id.firstEditText );
String str = editText().getText().toString();
// Here Str will be null blank all the time, since you haven't input anything in your EditText
}
#Override
public void onClick ( View view )
{
// Assuming you have already put some text in edittext.
String newStr = editText().getText().toString();
}
}
I would suggest you to follow some steps to find out your actual problem
check that you have set right layout in the setContentView.
before getting text from edit text set some text in it and then get text form the EditText view. Check that it still printing null.
I think you have done some silly mistake because EditText.getText() never returns null.
NullPoint exception is thrown because you are converting a NULL value to a string
et1.getText() is null and you are doing
et1.getText().toString();
which is wrong.
Every EditText is initially NULL because it does not contain any value.
According to Android Docs
public Editable getText ()
Added in API level 1
Return the text the TextView is displaying.
Since the EditText has not been set any text hence its current value is null.
I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.
When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored successfully. However, the display activity (which displays the stored String values in SharedPreferences) doesn't display the values. I am guessing it is because it is the onCreate, so the screen is "created" when it runs. How can I get it to update the EditText values when the user commits (presses save) immediately?
CustomStoreEditActivity - Just storing the user inputs (EditText entries):
final Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (saveButton.isClickable()) {
SharedPreferences prefs = getSharedPreferences(
"customtime", 0);
// prefs.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences.Editor edit = prefs.edit();
EditText shopName = (EditText) findViewById(R.id.shopName);
EditText open1 = (EditText) findViewById(R.id.open1);
EditText close1 = (EditText) findViewById(R.id.close1);
EditText open2 = (EditText) findViewById(R.id.open2);
EditText close2 = (EditText) findViewById(R.id.close2);
EditText open3 = (EditText) findViewById(R.id.open3);
EditText close3 = (EditText) findViewById(R.id.close3);
EditText open4 = (EditText) findViewById(R.id.open4);
EditText close4 = (EditText) findViewById(R.id.close4);
EditText open5 = (EditText) findViewById(R.id.open5);
EditText close5 = (EditText) findViewById(R.id.close5);
EditText open6 = (EditText) findViewById(R.id.open6);
EditText close6 = (EditText) findViewById(R.id.close6);
EditText open7 = (EditText) findViewById(R.id.open7);
EditText close7 = (EditText) findViewById(R.id.close7);
EditText comments = (EditText) findViewById(R.id.comments);
edit.putString("shopName", shopName.getText().toString());
edit.putString("Monday1", open1.getText().toString());
edit.putString("Monday2", close1.getText().toString());
edit.putString("Tuesday1", open2.getText().toString());
edit.putString("Tuesday2", close2.getText().toString());
edit.putString("Wednesday1", open3.getText().toString());
edit.putString("Wednesday2", close3.getText().toString());
edit.putString("Thursday1", open4.getText().toString());
edit.putString("Thursday2", close4.getText().toString());
edit.putString("Friday1", open5.getText().toString());
edit.putString("Friday2", close5.getText().toString());
edit.putString("Saturday1", open6.getText().toString());
edit.putString("Saturday2", close6.getText().toString());
edit.putString("Sunday1", open7.getText().toString());
edit.putString("Sunday2", close7.getText().toString());
edit.putString("comments", comments.getText().toString());
edit.commit();
Intent myIntent = new Intent(getBaseContext(),
CustomStoreActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
Toast.makeText(getBaseContext(), "Opening Hours Saved!",
Toast.LENGTH_SHORT).show();
}
}
});
}
CustomStoreActivity - where I believe the problem lies:
private void displayPreferences(){
SharedPreferences prefs = getSharedPreferences("customtime", 0);
String shopName = prefs.getString("shopName", "Empty");
String shopTime1 = prefs.getString("Monday1", " ");
String shopTime2 = prefs.getString("Monday2", " ");
String shopComments = prefs.getString("comments", "");
TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs);
displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments);
}
Thank you for your ample time.
I think part of what you're looking for is to put your CustomStoreActivity code in onResume instead of onCreate. Whatever code you move into onResume will occur whenever the CustomStoreActivity is brought to the front (including when it is first created).
Another alternative, assuming that CustomStoreActivity is used to launch the Activity that contains the EditTexts, is to use startActivityForResult and pass the data back in the result Intent instead of using the SharedPreferences. Here is a simple example (though note that the setResult call in this example is passed null where you would want to pass in an Intent, as documented here in the Android docs).
It also seems like you're trying to use your second Activity like a dialog box with editable fields. If that's the case, yet another alternative is to actually use a variant of the Dialog class within your CustomStoreActivity class instead of creating another Activity to act like one. See the Android doc for dialogs.
I'm not sure I really understand your question but are you trying to update the TextView of a different Activity from the one you are in? In which case I don't think this can be done and you should use Intents to pass the data to the activity