Unable to retrieve EditText value using findViewById
I am developing a very basic Android Application, issue is when I am
trying to retrieve the value of EditText widget it's giving me empty a
string.
my code is as Follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button generateButton = (Button) findViewById(R.id.button1);
generateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//generateFibonacci();
setContentView(R.layout.activity_main);
EditText seedValue = (EditText) findViewById(R.id.editText1);
TextView fibonacciSeries = (TextView)
findViewById(R.id.textView3);
String a = seedValue.getText().toString();
try {
int seedNumber =
Integer.parseInt(seedValue.getText().toString());
for (int i = 0; i < 100; i++) {
seedNumber += seedNumber;
fibonacciSeries.append(Integer.toString(seedNumber));
}
} catch (Exception e) {
fibonacciSeries.append(e.toString());
}
}
});
}
and in activity.xml
<EditText
android:id="@+id/editText1"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="12dp"
android:hint="@string/hint"
android:ems="10" />
The other similar looking questions here on SO are usually getting solved
by correcting the layout, but I think I am already in the correct layout
as my controls are in the same layout which is the one I am initializing
i.e. R.layout.activity_main kindly help.
No comments:
Post a Comment