2012-07-26 30 views
3

我在整個android編程相當新,所以請裸露在我身邊, 我有一種方式,我知道是不是現在送禮儀的最好方式,但即時通訊發送他們作爲字符串,並將其轉換爲一個Int reciver方面,問題是,當我做到這一點崩潰我手機上的轉換....這就是我對我的發送方:我如何發送多個整數到一個新的活動?

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); 

這是什麼,是我的reciving方:

@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); 

我測試了一切,它的轉換導致應用程序崩潰,我希望somebo dy可以幫助我使它不會崩潰,或給我一個全新的方式發送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)); 
} 
+0

崩潰logcat在哪裏? – 2012-07-26 14:16:24

+0

它只是說應用程序「payme」(即我的應用程序的臨時名稱)已停止工作 – Hockeyman271 2012-07-26 14:19:07

+0

在兩個類中嘗試使用Log.i(「user1552475」,「 - >」+ message1 +「< - 」)並檢查日誌。 – cosmincalistru 2012-07-26 14:20:02

回答

4

你並不需要通過IntegersStrings在接受Activity轉換回。 Intents可以容納Integers以及Strings

通常只需添加數據,就像您:

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

,然後檢索從接收Activity的意圖你int如下。

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

Intents可不僅僅是Strings容納更多的數據。其實看看documentation。你可以看到,意圖能容納LongsDoublesFloatsParcelables

+0

這工作完全謝謝你! – Hockeyman271 2012-07-26 15:55:55

+0

我有另一個問題,出於某種原因,我的activity2沒有收到用戶輸入,當我在activity2上顯示int時,它始終是「getIntExtra(」foobar「,0)中的默認值;所以它只是保持報告爲0 – Hockeyman271 2012-07-26 17:51:20

+0

如果您使用字符串文字作爲您的密鑰而不是使用常量,那麼這可能歸因於拼寫錯誤。例如,調用'getIntExtra(「foosbar」,0);'將返回0,因爲沒有鍵值爲「foosbar」的值。 – 2012-07-26 17:57:08

0

將數據傳遞到另一個活動 「串」 value.useing意圖

Activityone

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

ACTIV從一個活動到另一個活動 「整數」 value.useing意圖

Activityone itytwo

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

通數據

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); 
0

嘗試封裝轉換部分/ 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 
} 

*或者更恰當地說,傳遞你的發送部分的整數值,並按原樣接收它們。

+0

當我嘗試這種方法時,我得到這個錯誤: 令牌「貓」的語法錯誤,捕獲預計 – Hockeyman271 2012-07-26 14:31:29

+0

@ user1552475其捕獲。這是一個錯字 – COD3BOY 2012-07-26 14:34:36

+0

這很有用,謝謝!我會嘗試一些其他的建議,所以它更合適一些。 – Hockeyman271 2012-07-26 15:36:27

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"); 
     } 
} 
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"); 

希望它有幫助

相關問題