我構建了兩個活動,MainActivity將一些變量傳遞給活動「計算」。這按預期工作,變量正確提交和接收。我現在想根據意圖「大小」的值創建整數「size_int」。發生在此行中的問題:使用在if語句中更改的變量
debug1.setText(size_int);
Eclipse的告訴我,我應該創建一個名爲「size_int」的局部變量。我不明白爲什麼「size_int」不能在這行中使用,因爲它已經在if語句中定義過了。你有什麼想法嗎?我認爲它有一些事情要做,變量「size_int」正在if語句中定義,但我不確定。
下面是完整的代碼:
package com.example.eggtimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Calculation extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculation);
// get Intents (Size, Temperature and yolk from Main Activity)
String size = getIntent().getExtras().getString("size");
String temperature = getIntent().getExtras().getString("temperature");
String yolk = getIntent().getExtras().getString("yolk");
if (size.equals("Small")) {
int size_int = 30;
}
// Debug Variables
TextView debug1 = (TextView) findViewById(R.id.textViewDebug1);
debug1.setText(size_int);
}
}
請閱讀關於變量範圍部分):)它失敗,因爲你在if語句中聲明瞭size_int ..如果你會得到錯誤呢?那麼debug1.setText(size_int)會是什麼?我認爲這將是空的順便說一句。 – deadfish