Issue
I'm having trouble adding a Button
to save text input in my editorActivity
class, which is an Activity
I designated to view, edit, and save a note item.
The class uses the back button (hardware) and app launcher icon to save the key/value
pair to a SharedPreferences
object. I want to add a Button
that will do the same.
I get an error in the console stating:
res\layout\activity_note_editor.xml:18: error: Error: No resource found that matches the given name (at 'text' with value '@string/Save').
I'm not sure what this implies. And would appreciate some guidance as to where I should to fix this error.
This is what I have so far:
private NoteItem note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor); // Load custom note edit layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Launcher icon becomes an options button = home
Intent intent = this.getIntent(); // Reference to intent object in first activity
note = new NoteItem();
note.setKey(intent.getStringExtra("key")); // retrieve key and saved in note object
note.setText(intent.getStringExtra("text")); // Retrieve text and save in note object
EditText et = (EditText) findViewById(R.id.noteText);
et.setText(note.getText());
et.setSelection(note.getText().length());
}
private void saveAndFinish() {
EditText et = (EditText) findViewById(R.id.noteText);
String noteText = et.getText().toString();
Intent intent = new Intent();
intent.putExtra("key", note.getKey());
intent.putExtra("text", noteText);
setResult(RESULT_OK, intent);
finish();
}
@Override // launcher icon sends data back to main activity
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveAndFinish();
}
return false;
}
@Override // device back button sends data back to main activity
public void onBackPressed() {
saveAndFinish();
}
public void addListenerOnButton(){
Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveAndFinish();
}
});
}
}
Solution
I'm not sure what this implies.
It implies that you don't have a resource
in your strings.xml
with the identifier of "Save"
When you use @String
, or @anyAndroidResource for that matter, it looks in the corresponding file. So @drawable
would look inside the drawables
folder for whatever identifier you used.
In values/strings.xml
you should have something like
<string name="Save">Save</string>
Answered By - codeMagic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.