我的意图不起作用

时间:2016-04-29 23:37:34

标签: android android-intent

我是Android的佼佼者,这让我陷入困境。我在MainActivity.java上有这个Intent:

    Intent callLong = new Intent(MainActivity.this, LongActivity.class);
    callLong.putExtra("value1",value1);
    callLong.putExtra("value1",value2);
    callLong.putExtra("value1",value3);
    startActivity(callLong);

我声明了双值1,2和3,但是它说:“变量可能尚未初始化。”

在我的LongActivity.java中,我输入了以下代码:

Intent callLong = getIntent();

但它不起作用。请帮忙:(

这是整个代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void submitUnit (View view) {

    double value1,value2,value3;

    RadioButton radioButtonInit1 = (RadioButton) findViewById(R.id.unit_init1);
    assert radioButtonInit1 != null;
    RadioButton radioButtonInit2 = (RadioButton) findViewById(R.id.unit_init2);
    assert radioButtonInit2 != null;
    RadioButton radioButtonInit3 = (RadioButton) findViewById(R.id.unit_init3);
    assert radioButtonInit3 != null;
    RadioButton radioButtonFinal1 = (RadioButton) findViewById(R.id.unit_final1);
    assert radioButtonFinal1 != null;
    RadioButton radioButtonFinal2 = (RadioButton) findViewById(R.id.unit_final2);
    assert radioButtonFinal2 != null;
    RadioButton radioButtonFinal3 = (RadioButton) findViewById(R.id.unit_final3);
    assert radioButtonFinal3 != null;


    switch (view.getId()) {
        case (R.id.longitud):
            radioButtonInit1.setText(R.string.long1);
            radioButtonInit2.setText(R.string.long2);
            radioButtonInit3.setText(R.string.long3);
            radioButtonFinal1.setText(R.string.long1);
            radioButtonFinal2.setText(R.string.long2);
            radioButtonFinal3.setText(R.string.long3);
            value1 = 100;
            value2 = 39.37;
            value3 = 0.3937;
            break;
        case (R.id.area):
            radioButtonInit1.setText(R.string.area1);
            radioButtonInit2.setText(R.string.area2);
            radioButtonInit3.setText(R.string.area3);
            radioButtonFinal1.setText(R.string.area1);
            radioButtonFinal2.setText(R.string.area2);
            radioButtonFinal3.setText(R.string.area3);
            value1 = 10000;
            value2 = 1550;
            value3 = 0.155;
            break;
        case (R.id.volumen):
            radioButtonInit1.setText(R.string.volumen1);
            radioButtonInit2.setText(R.string.volumen2);
            radioButtonInit3.setText(R.string.volumen3);
            radioButtonFinal1.setText(R.string.volumen1);
            radioButtonFinal2.setText(R.string.volumen2);
            radioButtonFinal3.setText(R.string.volumen3);
            value1 = 1000000;
            value2 = 61023.74;
            value3 = 0.061023;
            break;
        case (R.id.masa):
            radioButtonInit1.setText(R.string.masa1);
            radioButtonInit2.setText(R.string.masa2);
            radioButtonInit3.setText(R.string.masa3);
            radioButtonFinal1.setText(R.string.masa1);
            radioButtonFinal2.setText(R.string.masa2);
            radioButtonFinal3.setText(R.string.masa3);
            value1 = 0.001;
            value2 = 0.03527;
            value3 = 35.2739;
            break;
        case (R.id.velocidad):
            radioButtonInit1.setText(R.string.vel1);
            radioButtonInit2.setText(R.string.vel2);
            radioButtonInit3.setText(R.string.vel3);
            radioButtonFinal1.setText(R.string.vel1);
            radioButtonFinal2.setText(R.string.vel2);
            radioButtonFinal3.setText(R.string.vel3);
            value1 = 3.6;
            value2 = 2.2369;
            value3 = 0.62137;
            break;
        case (R.id.tiempo):
            radioButtonInit1.setText(R.string.tiempo1);
            radioButtonInit2.setText(R.string.tiempo2);
            radioButtonInit3.setText(R.string.tiempo3);
            radioButtonFinal1.setText(R.string.tiempo1);
            radioButtonFinal2.setText(R.string.tiempo2);
            radioButtonFinal3.setText(R.string.tiempo3);
            value1 = 24;
            value2 = 1440;
            value3 = 60;
            break;
        case (R.id.fuerza):
            radioButtonInit1.setText(R.string.fuerza1);
            radioButtonInit2.setText(R.string.fuerza2);
            radioButtonInit3.setText(R.string.fuerza3);
            radioButtonFinal1.setText(R.string.fuerza1);
            radioButtonFinal2.setText(R.string.fuerza2);
            radioButtonFinal3.setText(R.string.fuerza3);
            value1 = 0.2248;
            value2 = 0.10197;
            value3 = 0.453592;
            break;
        case (R.id.presion):
            radioButtonInit1.setText(R.string.presion1);
            radioButtonInit2.setText(R.string.presion2);
            radioButtonInit3.setText(R.string.presion3);
            radioButtonFinal1.setText(R.string.presion1);
            radioButtonFinal2.setText(R.string.presion2);
            radioButtonFinal3.setText(R.string.presion3);
            value1 = 0.01;
            value2 = 0.009865;
            value3 = 0.9869;
            break;
        case (R.id.energia):
            radioButtonInit1.setText(R.string.energia1);
            radioButtonInit2.setText(R.string.energia2);
            radioButtonInit3.setText(R.string.energia3);
            radioButtonFinal1.setText(R.string.energia1);
            radioButtonFinal2.setText(R.string.energia2);
            radioButtonFinal3.setText(R.string.energia3);
            value1 = 0.238845;
            value2 = 0.947817;
            value3 = 3.96832;
            break;
        case (R.id.moneda):
            radioButtonInit1.setText(R.string.moneda1);
            radioButtonInit2.setText(R.string.moneda2);
            radioButtonInit3.setText(R.string.moneda3);
            radioButtonFinal1.setText(R.string.moneda1);
            radioButtonFinal2.setText(R.string.moneda2);
            radioButtonFinal3.setText(R.string.moneda3);
            value1 = 0.14463;
            value2 = 0.126292351;
            value3 = 0.87320992;
            break;
    }

    Intent callLong = new Intent(MainActivity.this, LongActivity.class);
    callLong.putExtra("value1",value1);
    callLong.putExtra("value1",value2);
    callLong.putExtra("value1",value3);
    startActivity(callLong);
}

}

1 个答案:

答案 0 :(得分:0)

您应该在switch语句中添加默认选项,并将默认值设置为变量value1value2value3,而不是view.getId()的值不匹配任何案例选项。 例如:

....
case (R.id.moneda):
            radioButtonInit1.setText(R.string.moneda1);
            radioButtonInit2.setText(R.string.moneda2);
            radioButtonInit3.setText(R.string.moneda3);
            radioButtonFinal1.setText(R.string.moneda1);
            radioButtonFinal2.setText(R.string.moneda2);
            radioButtonFinal3.setText(R.string.moneda3);
            value1 = 0.14463;
            value2 = 0.126292351;
            value3 = 0.87320992;
            break;
default: 
            value1= 0;
            value2= 0;
            value3= 0;
} // end of switch sentence