android-如何将多个整数发送到新活动?

android-如何将多个整数发送到新活动?,第1张

概述我知道我现在不是通过rite发送它们的最佳方法,但是我将它们作为字符串发送并将其转换为接收方的Int,问题是当我进行转换时,它在手机上崩溃了.这是我在发件人方面的东西:publicvoidsendMessage(Viewview){//DosomethinginresponsetobuttonIntentintent=

我知道我现在不是通过rite发送它们的最佳方法,但是我将它们作为字符串发送并将其转换为接收方的Int,问题是当我进行转换时,它在手机上崩溃了.这是我在发件人方面的东西:

    public voID sendMessage(VIEw vIEw) {    // Do something in response to button    Intent intent = new Intent(this, PayTracker.class);    // hourly wage send    EditText editText = (EditText) findVIEwByID(R.ID.hourly_wage);    String message1 = editText.getText().toString();    intent.putExtra(MESSAGE_1, message1);    // ot wage send    EditText editText1 = (EditText) findVIEwByID(R.ID.ot_wage);    String message2 = editText1.getText().toString();    intent.putExtra(MESSAGE_2, message2);    // hours per day send    EditText editText2 = (EditText) findVIEwByID(R.ID.hours_day);    String message3 = editText2.getText().toString();    intent.putExtra(MESSAGE_3, message3);    // start new activity    startActivity(intent);

这就是我接受的一面:

    @OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_pay_tracker);    getActionbar().setdisplayHomeAsUpEnabled(true);    // Receive messages from options page    Intent intent = getIntent();    String message1 = intent.getStringExtra(Options.MESSAGE_1);    String message2 = intent.getStringExtra(Options.MESSAGE_2);    String message3 = intent.getStringExtra(Options.MESSAGE_3);    // convert string to integer    int HW = Integer.valueOf(message1);    int OTW = Integer.valueOf(message2);    int HPD = Integer.valueOf(message3);

我已经测试了所有内容及其导致应用程序崩溃的转换,我希望有人可以帮助我确保它不会崩溃,或者为我提供一种全新的方式,将int发送给我的第二个活动,即发送字符串并进行转换.
谢谢!

================================================== =====================

这是我的新代码,在您的帮助下!

正在发送:

    public voID sendMessage(VIEw vIEw) {    // Do something in response to button    Intent intent = new Intent(this, PayTracker.class);    // Gather text from text Boxes    EditText editText = (EditText) findVIEwByID(R.ID.hourly_wage);    EditText editText1 = (EditText) findVIEwByID(R.ID.ot_wage);    EditText editText2 = (EditText) findVIEwByID(R.ID.hours_day);    //Create String from text    String message1 = editText.getText().toString();    String message2 = editText1.getText().toString();    String message3 = editText2.getText().toString();    //Convert String to Int    int HW = 0, OTW = 0, HPD = 0;    try{        HW = Integer.valueOf(message1);        OTW = Integer.valueOf(message2);        HPD = Integer.valueOf(message3);    }    catch(NumberFormatException nfe){        //do something else here        //for e.g. initializing default values to your int variables    }    // Send Integers to PayTracker.java    intent.putExtra(MESSAGE_HW, HW);    intent.putExtra(MESSAGE_OTW, OTW);    intent.putExtra(MESSAGE_HPD, HPD);    // start new activity    startActivity(intent);}

接收方:

    @OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_pay_tracker);    getActionbar().setdisplayHomeAsUpEnabled(true);    // Receive messages from options page    Intent intent = getIntent();    int HW = intent.getIntExtra(Options.MESSAGE_HW, 0);    int OTW = intent.getIntExtra(Options.MESSAGE_OTW, 0);    int HPD = intent.getIntExtra(Options.MESSAGE_HPD, 0);    // set textvIEw    TextVIEw textVIEw = (TextVIEw) this.findVIEwByID(R.ID.yourpay);    textVIEw.setText(String.valueOf(HW));}

解决方法:

您无需将Integers作为字符串传递来在接收Activity中转换回去. Intent可以包含Integer和String.

只需像通常那样添加数据:

int foo = 5;Intent intent = new Intent(this, bar.class);intent.putExtra("foobar", foo);

然后从接收活动的意图中检索您的int,如下所示.

Intent intent = getIntent();int foo = intent.getIntExtra("foobar", 0);

意图不仅可以容纳字符串,还可以容纳更多数据.实际上,请看一下documentation.您可以看到Intent可以容纳Longs,Doubles,floats,Parcelables等.

总结

以上是内存溢出为你收集整理的android-如何将多个整数发送到新活动?全部内容,希望文章能够帮你解决android-如何将多个整数发送到新活动?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085706.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存