我有一个名为MyPrimaryClass的类,这个类在按下时有一个按钮,用类myClassForResult创建一个Intent。来自资源字符串的Toast.makeText
我使用它来启动它:
startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);
两个MyPrimaryClass和myClassForResult延伸活动。
因此,当我在myClassForResult中调用Toast.makeText时,使用R.string.my_resource_string的文本参数,它给了我强制关闭!
我曾尝试这样的:
Context c = myClassForResult.this;
Toast toast = Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT);
toast.show();
同样在此:C = getApplicationContext()
同样在此:C = getBaseContext()
同样在此:
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_SHORT);
toast.show();
如果我使用内联字符串,比如“My Toast Text!”,它就可以工作。但我需要从资源中获取一个字符串。
- 问题解决:
为了解决我改变了敬酒的持续时间Toast.LENGTH_LONG
字符串R.string.my_resource_string值是问题“的标题为空”
当我将它的值更改为“The title”时,它工作正常,所以我猜这个字符串Toast.LENGTH_SHORT持续时间太长。
但是,当我将持续时间更改为Toast.LENGTH_LONG时,我可以使用长字符串。
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_LONG);
toast.show();
发布强制关闭的logcat错误。 – JoxTraex 2012-02-05 20:28:54
这是logcat错误: android.content.res.Resources $ NotFoundException:String资源ID#0x7f04000a – DMF 2012-02-05 20:36:50
你能解释为什么你要使用这两个类的意义? – JoxTraex 2012-02-05 20:38:25