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.
Related
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
I got null data while fetch data from text Box.
My Code is:
EditText msg=(EditText)findViewById(R.id.Et_Msg);
String setMsg=msg.getText().toString();
Log.v("Messge","Message::"+setMsg);
Please tell me where I am wrong ?
This is your code,
EditText msg=(EditText)findViewById(R.id.Et_Msg);
String setMsg=msg.getText().toString();
Log.v("Messge","Message::"+setMsg);
The first line is initializing the EditText. When it does by default there is no value ( string ) in the edittext.
In the second line you are trying to fetch a string from a blank edittext, that's why it is giving you NullPointerException.
Solution : I suggest you to move your line String setMsg=msg.getText().toString(); to somewhere else where you are actually going to use the value of EditText.
While getting your data from EditText, you must create a listener orelse you get the value as null for an example button click listener..
For an example:
public class A extends Activity implements OnClickListener{
Button btn;
EditText edt;
#Override
public void onCreate(Bundle saved){
super.onCreate(saved);
edt = (EditText) findViewById(R.id.your_id);
btn = (Button) findViewById(R.id.your_id);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v){
if(v == btn){
String setMsg=edt.getText().toString();
Log.v("Messge","Message::"+setMsg);
}
}
}
see.. what you are doing.. immediately after obtaining and EditText's object you are calling getText() on it.. think logically.. obviously there is nothing (it should return blank though not sure why it is returing null) in the EditText unless you have it from the xml.. something like this;
<EditText
...
android:text="Hey there"
...
/>
try this.. or move getText() call under some button click..
Please replace your below line
String setMsg=msg.getText().toString();
with
String setMsg = String.valueOf(msg.getText());
Please try above line. Your problem will be solved.
In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.
I have a program that has a text input and a button. When I type something into the input and press the button I want that String to be added to a String Arraylist and have that Arraylist displayed in a TextView. Right now I have a method:
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
"edit-choices" is the name of the text input and "choices" is the name of the array list. First of all am I doing this correctly? Second, how to I get the text view to display the contents of "choices". Right now my TextView id is just textView1
Please keep in mind that it is not the best way to show list items in a TextView. You can do this using a ListView. Anyhow, see pseudo code below (didn't test that in Eclipse, however, it should show how it is basically going to work):
public class YourActivity extends Activity {
Vector<String> choices = new Vector<String>();
public void onCreate(Bundle ....) {
(Button) myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean button.onClick() {
addString();
TextView textView = (TextView) findViewById(R.id.text_view);
String listRepresentation = "";
for (String choice : choices)
if ("".equals(listRepresentation))
listRepresentation = choice; else
listRepresentation = ", " +choice;
textView.setText(listRepresentation );
}
});
}
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
}
So simply assign an OnClickListener to your button that does what you need.
The question is how you want the Text to be displayed...
Either like a list view or just as a normal text.
If you want to show the text as a normal text in the text view you can simply do something like this.
for(String msg : choices)
{
textView1.setText(textView1.getText()+msg);
}
If you want the choices to be displayed in list view you need to set an adapter to the list view using the choices that you have.
First of all am I doing this correctly?
If it works for you, sure. I would maybe cache the EditText so you don't have to "find" it every time you want to access it's content.
Your only "problem" here is, that a TextView has no method that accepts a List<String>. So, you'll need to make a single string out of your list of strings.
You can simply iterate over the list and con-cat them together:
StringBuilder b = new StringBuilder();
for (String s : choices){
b.append(s+"\n");
}
textview.setText(b.toString());
This will simply build one string from all the items in your list, adding line-breaks after every item.
You'll need to set your TextView's android:inputType-attribute to textMultiLine, so it will actually show you multiple lines.
I'm trying to write my first Android app. It will take a number input by a user in an EditText field, convert it to an integer, then find the factors. I want to port this from a Java program that I wrote before. I have stubs working to the point that I have a UI, but I haven't yet ported the code that will find the factors. I'm stuck trying to convert the EditText to an integer. If I insert either of the following lines, the program crashes in the emulator. Log.Cat says, "Caused by NumberFormatExcepion: Unable to parse '' as an integer."
Any suggestions are appreciated.
userNumber is the name of the value taken from the EditText field, and the EditText field is also named userNumber. I don't know if that's bad form or not. I want to assign the value of userNumber to the integer value userInt. userInt will then be factored.
Either of these approaces will cause the problem:
userNumber = (EditText) findViewById(R.id.userNumber);
userInt = Integer.parseInt(userNumber.getText().toString());
Integer userInt = new Integer(userNumber.getText().toString());
The EditText block of XML looks like this:
<EditText
android:id="#+id/userNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
Here is the relevant code from the class:
public class AndroidFactoringActivity extends Activity {
// Instance Variables
EditText userNumber;
Button factorButton;
TextView resultsField;
int factorResults = 1;
int userInt = 0; // This comes out if using Integer userInt
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultsField = (TextView) findViewById(R.id.resultsField);
factorButton = (Button) findViewById(R.id.factorButton);
userNumber = (EditText) findViewById(R.id.userNumber);
// userNumber is also the name of the EditText field.
// userInt = Integer.parseInt(userNumber.getText().toString());
// Integer userInt = new Integer(userNumber.getText().toString());
resultsField.append("\n" + String.valueOf(userInt));
//Later, this will be factorResults, not userInt.
// Right now, I just want it to put something on the screen.
}
}
You're trying to parse the int in your onCreate method, which occurs before the user has a chance to enter anything into the EditText. Hence the exception from trying to parse an empty string.
You'll have to either make a button to press, which will then parse the int out of the EditText, or attach a listener to the EditText that will parse it when something is typed into it.