java 在吐司上显示整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28152958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:05:39  来源:igfitidea点击:

Displaying integer on toast

javaandroidtoast

提问by Gal Israel

Im trying to display a toast message with integer inside it This is how i tried to do it:

我试图在其中显示一个带有整数的 toast 消息这是我尝试这样做的方式:

 Toast.makeText(this,bignum,Toast.LENGTH_LONG).show();

But it keeps crash my app. Thanks for help!

但它让我的应用程序崩溃。感谢帮助!

回答by Mena

Toast.makeTexteither takes a CharSequenceor an intas its second argument.

Toast.makeText将 aCharSequence或 anint作为其第二个参数。

However, the intrepresents a resource ID(such as R.string.hello_world).

但是,int表示资源 ID(例如R.string.hello_world)。

The application crashes probably because no resource is found with that ID, since it's not an ID to start with, but an arbitrary integer.

应用程序崩溃可能是因为没有找到具有该 ID 的资源,因为它不是一个 ID,而是一个任意整数。

In your case, use Toast.makeText(this,String.valueOf(bignum),Toast.LENGTH_LONG).show();.

在您的情况下,请使用Toast.makeText(this,String.valueOf(bignum),Toast.LENGTH_LONG).show();.

回答by Blackbelt

you need a String

你需要一个 String

Toast.makeText(this, String.valueOf(bignum),Toast.LENGTH_LONG).show();

otherwise android will try to look it up for a String with id bignum, in your strings.xml file

否则 android 将尝试bignum在您的 strings.xml 文件中查找带有 id 的 String

回答by Nick

Try this to "cast" bignum to string:

试试这个将 bignum 转换为字符串:

Toast.makeText(this,"" + bignum,Toast.LENGTH_LONG).show();

回答by Ziad H.

You can do this:

你可以这样做:

Toast.makeText(getBaseContext(), "" + bignum, Toast.LENGTH_LONG).show();