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));
Related
Hy, I wanna ask something about dynamic EditText that's added by button click.
I have another scanner Button to scan qr code, get the value and set the value in each EditText added. To set the text I sure need to know the id of each EditText. So how do I assign id to each EditText or there's another way to work around with it.
Inside my add-button onclick
public void addView(View view){
LinearLayout li = (LinearLayout) findViewById(R.id.etMsisdn);
edt = new EditText(this);
edt.setId(0);
li.addView(edt);
}
Thank you very much
Hope to helpful
String strname = "task" + Integer.toString(i);
EditText editText = new EditText(this);
editTextsMap.put(strname, editText);
You can set a tag for your dynamically created EditText with calling setTag(Object tag) method. Then, you can simply find it with calling findViewWithTag() method.
// Dynamically create EditText
EditText editText = new EditText(this);
editText.setTag("editText");
// Find it via tag
EditText editText = (EditText) findViewWithTag("editText");
Pretty sure this is a stupid question but I can't figure this out.
I am trying to get an integer returned by a datepicker to a string. This code works where day is the integer of interest
dateButton = (Button) findViewById(R.id.dateButton);
dateButton.setText((Integer.toString(day));
This code gives me the error that cannot resolve method setText
String yearString = "";
yearString.setText(Integer.toString(year));
I don't understand why I cant convert the int to a string unless I use a view?
Is this, what you want to do.
int year = 2014;
String yearString = Integer.toString(year);
Because stText mthod is only for setting text on certain views on android likeTextView, EditText, Button.
You can set integer value by following ways if day is an integer value,
dateButton.setText(day+"");
or by
dateButton.setText(String.valueOf(day));
or
dateButton.setText(Integer.toString(day));
dateButton = (Button) findViewById(R.id.dateButton);
dateButton.settext(Interger.Valueof(day));
you have to instantiate the object before you call it view I am new to this also
So this
((TextView) findViewById(R.id.now_playing_text)).setText(trackTitle)
Becomes
TextView Title = (TextView) findViewById(R.id.now_playing_text);
Title.setText(trackTitle);
setText must be applied on an a class that contains in it or it supper classes the setText method.
Instead of
dateButton.setText((Integer.toString(day));
Try this
dateButton.setText(day+"");
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.
basically what I've tried, is to create a XML with two input types decimal. Input A and Input B, and when I click add, these values will get passed onto a class method, and that method will do so and so.
The problem I have is that I have no way of passing the value from an XML file to a java file (I think)
I spent like an hour doing the XML thing only to find out I can't do it.
Can you possibly guide me as to how I might go about doing this?
EditText mEditText1 = (EditText)findViewById(R.id.number1i);
String val1 = mEditText1.getText().toString();
EditText mEditText2 = (EditText)findViewById(R.id.number2i);
String val2 = mEditText2.getText().toString();
Your XML contains two editText and one button?
in your java file you should do:
EditText e1 = (EditText)findViewById(R.id.e1);
EditText e2= (EditText) findViewById(R.id.e2);
and in xml set onClick attribute on button (example onClick="onClick")
in code:
public void onClick(View v){
e1.getText.toString(); e2.getText.toString(); //save it to string and
you have what you want}
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.