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

时间:2012-07-26 14:13:51

标签: android string android-activity int

我有一种方法,我知道现在不是通过仪式发送它们的最佳方式,但是我将它们作为字符串发送并将它们转换为reciver端的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);

这就是我的回忆:

    @Override
public 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);
}

接收方:

    @Override
public 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));
}

5 个答案:

答案 0 :(得分:4)

您无需将Integers作为Strings传递回接收ActivityIntents可以Integers以及Strings

只需像往常一样添加数据:

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

然后从接收int中的意图中检索您的Activity,如下所示。

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

Intents可以保存的数据不仅仅是Strings。事实上,看看documentation。您可以看到Intent可以保留LongsDoublesFloatsParcelables等。

答案 1 :(得分:0)

将数据从一个活动传递到另一个活动“String”value.useing intent

Activityone

Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent); 

Activitytwo

Intent intent =getIntent();
Receivevalue =intent.getExtras().getString("TYPE");
(OR)
Receivevalue = getIntent().getExtras().getString("TYPE");

将数据从一个活动传递到另一个活动“整数”值。使用意图

Activityone

Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent); 

Activitytwo

Intent intent = getIntent();
value = intent.getIntExtra("TYPE", 0);     
// Type = intent.getIntExtra(name, defaultValue);

答案 2 :(得分:0)

将转换部分封装在 try / catch 块中,因为您的字符串可能无法转换为整数值。

int HW, OTW, HPD;

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
}

*或者更恰当的是,传递发送部分中的整数值并按原样接收它们。

答案 3 :(得分:0)

在发送方尝试这样的事情:

 String message1 = editText.getText().toString();   
 intent.putExtra("MESSAGE_1", message1);
 String message2 = editText1.getText().toString();
 intent.putExtra("MESSAGE_2", message2);
 String message3 = editText2.getText().toString();
 intent.putExtra("MESSAGE_3", message3);

接收方:

  if (getIntent() != null) {
    if (getIntent().getExtras() != null) {
        String mess1 = getIntent().getExtras().getString("MESSAGE_1"); 
        String mess2 = getIntent().getExtras().getString("MESSAGE_2"); 
        String mess3 = getIntent().getExtras().getString("MESSAGE_3"); 
      }
 }

答案 4 :(得分:0)

您可以使用以下代码

来实现目标

在您的通话活动中

         Intent value = new Intent(YourCallingActivity.this,DestinationActivity.class);
         value.putExtra("integerone", integeronevalue);
         value.putExtra("integertwo", integertwovalue);
         value.putExtra("integerthree", integertwovalue);
         startActivity(value);

目的地活动

        int integerone = getIntent().getExtras().getInt("integerone");
        int integertwo = getIntent().getExtras().getInt("integertwo");
        int integerthree = getIntent().getExtras().getInt("integerthree");

希望有所帮助