如何正确使用字符串资源?

时间:2021-03-25 17:09:08

标签: android string

我在尝试用 strings.xml 中的等效项替换所有硬编码字符串时遇到此错误。显然人们在尝试使用整数时遇到了这个错误,但在这种情况下我所有的字符串都是字符串。

错误:

2021-03-25 19:48:25.233 676-676/com.asdf.asdf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.asdf.asdf, PID: 676
android.content.res.Resources$NotFoundException: String resource ID #0x7f0f0002
    at android.content.res.Resources.getText(Resources.java:360)
    at android.content.res.MiuiResources.getText(MiuiResources.java:97)
    at android.content.res.Resources.getString(Resources.java:453)
    at com.emirhalici.myenglishdictionary.AddWordAdapter$eViewHolder$1.onClick(AddWordAdapter.java:44)
    at android.view.View.performClick(View.java:6608)
    at android.view.View.performClickInternal(View.java:6585)
    at android.view.View.access$3100(View.java:785)
    at android.view.View$PerformClick.run(View.java:25921)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:6878)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

strings.xml 文件:

<resources>
<string name="app_name">MyEnglishDictionary</string>
<string name="Home">Home</string>
<string name="Quiz">Quiz</string>
<string name="AddWord">Add Word</string>
<string name="ToastErrorResponse">Error while getting response.</string>
<string name="TextViewAddHint">Enter the word you\'re looking for.</string>
<string name="btn_add_manually">Add Manually</string>
<string name="btn_search_word">Search Word</string>
<string name="No">No</string>
<string name="Yes">Yes</string>
<string name="AddWordAlertDialogTitle">Are you sure?</string>
<string name="AddWordSuccess">Word added to database successfully.</string>
<string name="AddWordAlertDialogMessage">The word %1$s will be added to dictionary.</string>

相关java代码:

        public eViewHolder(@NonNull View itemView) {
        super(itemView);
        tv_type = (TextView) itemView.findViewById(R.id.tv_type);
        tv_word = (TextView) itemView.findViewById(R.id.tv_word);
        tv_definition = (TextView) itemView.findViewById(R.id.tv_definition);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(context);
                String alertTitle = Resources.getSystem().getString(R.string.AddWordAlertDialogTitle);
                dialogBuilder.setTitle(alertTitle);

                String word = mWordList.get(getAdapterPosition()).getWord();
                String alertMessage = Resources.getSystem().getString(R.string.AddWordAlertDialogMessage, word);
                dialogBuilder.setMessage(alertMessage);

                dialogBuilder.setPositiveButton(Resources.getSystem().getString(R.string.Yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatabaseHelper databaseHelper = new DatabaseHelper(context);
                        databaseHelper.addOne(mWordList.get(getAdapterPosition()));
                        Toast.makeText(context, Resources.getSystem().getString(R.string.AddWordSuccess), Toast.LENGTH_SHORT).show();
                    }
                });
                dialogBuilder.setNegativeButton(Resources.getSystem().getString(R.string.No), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                dialogBuilder.show();

            }
        });
    }

AddWordAdapter.java:44 是行

String alertTitle = Resources.getSystem().getString(R.string.AddWordAlertDialogTitle);

编辑: 出现问题是因为我使用的是 Resources.getSystem().getString() 而不是 context.getResources().getString() em>。现在问题解决了。谢谢迈克尔。我似乎无法将此帖子标记为已解决,我是新来的。

1 个答案:

答案 0 :(得分:0)

正如@Micheal 所指出的,我使用 Resources.getSystem() 从资源中获取字符串。相反,我应该使用上下文来调用 getResources()。一个简单的修复应该是这样的。

String alertMessage = Resources.getSystem().getString(R.string.AddWordAlertDialogMessage, word); // error
String alertMessage = context.getResources().getString(R.string.AddWordAlertDialogMessage, word);