java - 保存按钮|笔记应用

标签 java android android-resources

我在添加 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>

Resources Docs

Resources Overview Docs

关于java - 保存按钮|笔记应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066365/

相关文章:

android - ActionBar 向上指示器位于何处

java - 改变变量的值?

java - 设置安卓 :windowIsFloating programmatically

java - 从 CDI 托管 bean 获取方法注释

java - 在我的例子中, for() 循环有什么问题?

java - 没有提供者的服务提供者接口(interface)

sdk - Android - 为 SDK 5 构建应用程序?

Android Sherlock 操作栏兼容性问题

java - 测试应用程序时出错 - Firebase 测试实验室

android - 如何减少水平 Flatlist 中项目之间的空间