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.
Related
I'm new in android world and I have a problem, well, I'm making a project very simple it is about an activity where i have a button and an EditText. The button has an event onClick in XML.
My problem is it: I need the button value and send this value to
EditText but my button don't have a id. Help me I don't know how manipulate a element if it dont have a id.
XML Code:
<View
android:layout_height="#dimen/cell_height"
android:background="#color/red"/>
<Button
android:layout_marginLeft="#dimen/button_margin"
android:layout_gravity="center_vertical"
android:text="#string/hex_red"
android:textColor="#color/red"
android:onClick="copy"/>`
Java code:
public void copy(View boton){
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
String color = boton; <-- here need the button value
txtSelected.setText(color);
}
I need your help, Thanks
you can say boton.getText().toString()
Modify your copy() function like this:
public void copy(View boton) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button) boton; // << key point.
String color = btn.getText().toString();
txtSelected.setText(color);
}`
public void onClickBtn(View view) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button)(view);
String value = (String) btn.getText();
txtSelected.setText(value);
txtSelected.setSelection(value.length()); // cursor will be at the end of the text
}
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.
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 am developing an application for Android.
My application contains ten buttons, to which I have set an onclicklistener() method.
The ten buttons contains the digits 0-9.
Now, if I click any two or three buttons among the ten buttons, the corresponding digits must be entered into edit text and it must be shown in the edittext box.
I am able to display the single digit if I click on any of the buttons, but if I click on another button, then the previous value disappears and the new value is shown.
But what I want is this: no matter how many buttons I click, that no. of digits will appear in the edittext box.
Please can anyone explain to me the code, or give me a hint so that it can be made in a simpler way.
Using Shared Preferences:
I think, you may used Shared Preferences when you button was click, get value from Edittext and put on shared preferences. After click next button get that shared preferences value. You may used each button click put on value shared preferences.
Go to this problem, which is help you to solve: >> SharedPreference problem in android
Using Intent:
May be used this code on button click event:
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
EditText text1 = (EditText) findViewById(R.id.EditText01);
EditText text2 = (EditText) findViewById(R.id.EditText02);
text1.setText(value1);
text2.setText(value2);
}
Other useful resources:
Get Value of a Edit Text field
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-edittext-controls/
http://www.java2s.com/Code/Android/UI/GetvaluefromEditText.htm
http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx
You are using editText.setText("");
Instead you must use editText.append();
You can take a public static String variable and concat the new value to previous and set it to EditText
Use below code
public class AsActivity extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
et=(EditText)findViewById(R.id.editText1);
}
public void onclick(View v){
switch(v.getId()){
case R.id.button1:
et.append("1");
break;
case R.id.button2:
et.append("2");
break;
case R.id.button3:
et.append("3");
break;
}
}
}
here i have use property of button you can use switch statement as shown above in ur onclicklistener
In all 0-9 buttons onclick event you can write following code.
editText.setText((editText.getText().toString) +""+ nevText);
When you click on button then above code set newText with previous text in edittext box.
You should use the EditText append() method which appends data to the EditText.
So each time a new button is clicked just use :
myEdtiText.append(str);
Hi im new to android and I have a program that has a global variable define and it works, so I can set it and get it in every activity, BUT it dosnt like to be changed in an on click listener. I made it so on the screen there is an edittext and when someone presses a button I want the edittext text to be put into the global variable. here is my code:
Button SiteButton = (Button) findViewById(R.id.SiteButton);
SiteButton.setOnClickListener(new View.OnClickListener() {
TextView textviewS = (TextView) findViewById(R.id.SiteIdT);
EditText edittextS = (EditText) findViewById(R.id.SiteIdE);
TextView textviewB = (TextView) findViewById(R.id.BusIdT);
EditText edittextB = (EditText) findViewById(R.id.BusIdE);
public void onClick(View v) {
textviewS.setText(edittextS.getText());
((Global) this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) this.getApplication()).setgVehicleId(textviewB.getText().toString());
}
});
but the getApplication() part is showing an error. can anyone help?
You should refer to your activity this, since View.OnClickListener doesn't have such a method:
// Bad code! read below
((Global) MyActivityClassName.this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) MyActivityClassName.this.getApplication()).setgVehicleId(textviewB.getText().toString());
By the way, how do you cast the return from getApplication() to Global? You will get a class cast exception there.