来自资源字符串的 Toast.makeText

发布于 2025-01-02 21:17:32 字数 1357 浏览 2 评论 0原文

我有一个名为 MyPrimaryClass 的类,该类有一个按钮,按下时会创建一个类为 myClassForResult 的 Intent。

我用它来启动它:

startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);

MyPrimaryClass 和 myClassForResult 都扩展了 Activity。

因此,当我在 myClassForResult 中使用 R.string.my_resource_string 的文本参数调用 Toast.makeText 时,它会强制关闭!

我已经尝试过这个:

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 的持续时间更改为 Toast.LENGTH_LONG

字符串 R.string.my_resource_string 值为“标题为空”,

当我将其值更改为“标题”,它工作正常,所以我猜字符串对于 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();

I have a class named MyPrimaryClass, this class has a button witch when pressed, creates an Intent with the class myClassForResult.

I use this to start it:

startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);

Both MyPrimaryClass, and myClassForResult extends Activity.

So, when I call Toast.makeText within the myClassForResult, with the text parameter of R.string.my_resource_string, it gives me Force Close!

I have tried this:

Context c = myClassForResult.this;
Toast toast = Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT);
toast.show();

Also this: c = getApplicationContext()

Also this: c = getBaseContext()

Also this:

Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
    R.string.my_resource_string,
    Toast.LENGTH_SHORT);
toast.show();

If I use an inline string, like "My toast Text!", it works. But i need to get a string from the resources.

-Problem solved:

To solve the problem I changed the duration of the Toast to Toast.LENGTH_LONG

The string R.string.my_resource_string value is "The title is empty"

When I change its value to "The title", it worked properly, so I guess the string was too long for the Toast.LENGTH_SHORT duration.

But when i change the duration to Toast.LENGTH_LONG, I could use the long string.

Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
    R.string.my_resource_string,
    Toast.LENGTH_LONG);
toast.show();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丿*梦醉红颜 2025-01-09 21:17:32

需要注意的一件事:

Toast toast = Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT);
toast.show();

可以简化为:

Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT).show();

这为您节省了不需要的对象引用。

您需要了解的一件事是,每当您在包中引用 R(而不是 android.R.)时,只要您拥有 Context,您就可以访问您的资源。

更新

在意识到您使用它的用途后,我建议您
改变你的方法
,虽然这实际上是可能的,但你的方法对于如此简单的事情来说并不理想。

startActivityForResult(xx) 方法通常适用于您想要启动包外部的应用程序以获取结果的情况。

例如:如果我想从产品中检索条形码,那么我会通过操作间接启动该条形码类的意图。然后我将通过使用 onActivityResult(xx) 检索数据。

为您自己的类执行此操作没有意义

One thing to note:

Toast toast = Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT);
toast.show();

Can be simplified into:

Toast.makeText(c,
    c.getResources().getString(R.string.my_resource_string),
    Toast.LENGTH_SHORT).show();

This saves you an object reference that you do not need.

One thing you need to understand is that whenever you reference you R in your package (not android.R.) you will have access to your resources as long as you have Context.

Update

After realizing what you are using this for I would recommend that you
change your approach
, while this is in fact possible, your approach isn't ideal for something so simple.

The method startActivityForResult(xx) is typically when you want to start an application that is outside of your package for a result.

For instance: if I wanted to retrieve a barcode from a product, then I'd start an Intent to that barcode class, indirectly through an action. Then I'd retrieve the data through using onActivityResult(xx).

it makes No Sense to do this for your own classes.

输什么也不输骨气 2025-01-09 21:17:32

尝试:

Toast.makeText(this, this.getString(R.string.my_resource_string), Toast.LENGTH_SHORT);

Try:

Toast.makeText(this, this.getString(R.string.my_resource_string), Toast.LENGTH_SHORT);
匿名的好友 2025-01-09 21:17:32

@dilipkaklotar 回答正确,但需要做一些更改:

这就是它对我的工作方式

Toast.makeText(getApplicationContext(),
getApplicationContext().getResources().getString(R.string.message),
Toast.LENGTH_SHORT).show();

getResources 没有括号 ()。
最后是 .show();不是 toShow()。

但这是正确的。非常感谢。

@dilipkaklotar Answered correctly but a few changes needs to be done:

this is how it worked for me

Toast.makeText(getApplicationContext(),
getApplicationContext().getResources().getString(R.string.message),
Toast.LENGTH_SHORT).show();

the getResources has no parenthesis ().
and at the end is .show(); not toShow().

but it's correct. so thank you very much.

酒儿 2025-01-09 21:17:32
Toast.makeText(getApplicationContext(), getApplicationContext().getResources.getString(R.string.imgval), Toast.LENGTH_SHORT).toShow();
Toast.makeText(getApplicationContext(), getApplicationContext().getResources.getString(R.string.imgval), Toast.LENGTH_SHORT).toShow();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文