How would i store a number/string into R.string.xx

2019-08-08 16:21发布

问题:

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);

回答1:

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.



回答2:

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.



回答3:

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

public View x = findViewById(R.string.nfoname);

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?

***x=(View) infoname.getText();***

You just use infoname.getText().toString() you will get the string value of the Edittext's current text. Dude you can do stuff simply.



回答4:

public View x = findViewById(R.string.nfoname);

This can't work as not only are you trying to find a View using a R.string resource id, you are doing it before setContenView(...) is called in your onCreate(...) method. Even if you used a valid View resource id such as R.id.infoname then x will be null because the content view hasn't been inflated yet.

final EditText infoname=(EditText)findViewById(R.id.infoname);

Apart from the pointless use of final this should'nt cause problems as long as R.id.infoname is actually the resource id of an EditText.

x=(View) infoname.getText();

Not only will x be null but calling getText() on an EditText returns an Editable which is not a View nor is it possible to cast it to View. Even if you used getText().toString() which is the correct way to get the text from an EditText it still wouldn't be possible to cast a String to a View.

Also, as for this...

TextView x = setText(R.string.nfoname);

It would have to be...

TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));