我在添加 Button
来保存 editorActivity
类中的文本输入时遇到问题,该类是我指定要查看的 Activity
,编辑并保存注释项。
该类使用后退按钮(硬件)和应用程序启动器图标将键/值
对保存到SharedPreferences
对象。我想添加一个具有相同功能的 Button
。
我在控制台中收到错误消息:
res\layout\activity_note_editor.xml:18: error: Error: No resource found that matches the given name (at 'text' with value '@string/Save').
我不确定这意味着什么。并希望得到一些关于我应该在哪里修复此错误的指导。
这是我到目前为止所拥有的:
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();
}
});
}
}
最佳答案
I'm not sure what this implies.
这意味着您的 strings.xml
中没有标识符为“Save”的资源
当您使用@String
或@anyAndroidResource时,它会在相应的文件中查找。因此,@drawable
会在 drawables
文件夹中查找您使用的任何标识符。
在 values/strings.xml
中,您应该有类似的内容
<string name="Save">Save</string>
关于java - 保存按钮|笔记应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066365/