with this code, my program just force close(error)
***public View x = findViewById(R.string.nfoname);***
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
the one with the * are the source of error
program function: if the user clicks confirm, his name will be set to R.string.nfoname which will then be used in another layout through TextView x = setText(R.string.nfoname);
At least in the case of your variable infoname scoping is most likely causing your application to throw an error. infoname is a local variable to the function onCreate(), not an instance variable for your class, so it can't be accessed by your onClick() methods because they are part of an anonymous class.
Another thing I'd question is why you marked infoname as final? It goes out of scope when onCreate() exits so if it gets changed, you can see who changed it since it only exists while the method is executing.
This can't work as not only are you trying to find a
View
using aR.string
resource id, you are doing it beforesetContenView(...)
is called in youronCreate(...)
method. Even if you used a validView
resource id such asR.id.infoname
thenx
will benull
because the content view hasn't been inflated yet.Apart from the pointless use of
final
this should'nt cause problems as long asR.id.infoname
is actually the resource id of anEditText
.Not only will
x
benull
but callinggetText()
on anEditText
returns anEditable
which is not aView
nor is it possible to cast it toView
. Even if you usedgetText().toString()
which is the correct way to get the text from anEditText
it still wouldn't be possible to cast aString
to aView
.Also, as for this...
It would have to be...
You cannot set values to R.string.xxx because all these values will be constants much like a read only stuff. If you want to use the value of edit text to another layout use class variables or
intent.putextra()
Coming to ur source code i see this
How can a view be found by R.String? This should be R.id.
final EditText infoname=(EditText)findViewById(R.id.infoname); Why this editText has to be final?
You just use
infoname.getText().toString()
you will get the string value of the Edittext's current text. Dude you can do stuff simply.I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.
I think what you would want to do is save the user input as a SharedPreference or in a database.
See:SharedPreferences on the android docs for an example usage.