如何将意图数据设置为当前活动editText?

时间:2013-09-27 15:40:35

标签: java android android-intent

我正在尝试将两个数字输入从一个活动转移到另一个活动的UI但是当我单击按钮更改意图时它会崩溃。

我在logcat中得到以下内容:http://pastebin.com/zkWPcSNZ,这表明我将数据解析为CalcResult中的editTexts的方式存在问题。

我的问题是我试图将数据传递给CalcResult editText的方式有什么问题。是否有另一种方法可以实现这一点?

我的两个课程看起来像这样参考:

public class MainActivity extends Activity implements OnClickListener {

    //variables for xml objects
    EditText offsetLength,offsetDepth,ductDepth;
    Button calculate;

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

        //setting the variables to the xml id's and setting the click listener on the calc button
        offsetLength = (EditText)findViewById(R.id.offLength);
        offsetDepth = (EditText)findViewById(R.id.offDepth);
        ductDepth = (EditText)findViewById(R.id.ductDepth);
        calculate = (Button)findViewById(R.id.calc);
        calculate.setOnClickListener(this);//don't cast the listener to OnClickListener


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        try {

            String getoffsetlength = offsetLength.getText().toString(); 
            String getoffsetdepth = offsetDepth.getText().toString(); 
            String getductdepth = ductDepth.getText().toString(); 

            double tri1,tri2;
            double marking1,marking2;

            double off1 = Double.parseDouble(getoffsetlength);
            double off2 = Double.parseDouble(getoffsetdepth);
            double off3 = Double.parseDouble(getductdepth)
                    ;
            marking1 = Math.pow(off1,2) + Math.pow(off2,2);
            tri1 = (float)off2/(float)off1;
            tri2 = (float)off3/Math.atan((float)tri1);
            marking2 = (float)off3/Math.atan(tri2);


            Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
            myIntent.putExtra("numbers", marking1);
            myIntent.putExtra("numbers", marking2);

            startActivity(myIntent);


        } catch (NumberFormatException e) {
            // TODO: handle exception
            System.out.println("Must enter a numeric value!");

        }

    }


}

这是我将数据传递给的活动:

public class CalcResult extends MainActivity
{
    EditText result1,result2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        result1 = (EditText)findViewById(R.id.mark1);
        result2 = (EditText)findViewById(R.id.mark2);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        double mark1 = bundle.getDouble("number1");
        double mark2 = bundle.getDouble("number2");

        int a = Integer.valueOf(result1.getText().toString());
        int b = Integer.valueOf(result2.getText().toString());

        result1.setText(a + "  ");
        result2.setText(b + "  ");

    }

}

3 个答案:

答案 0 :(得分:1)

当你开始第二个活动时,两个editText都是空的

所以当你这样做时:

int a = Integer.valueOf(result1.getText().toString());
int b = Integer.valueOf(result2.getText().toString());

它相当于:

int a = Integer.valueOf("");
int b = Integer.valueOf("");

抛出异常

Caused by: java.lang.NumberFormatException: Invalid int: ""

如果要将它们设置为通过两个活动传递的值,您可以这样做:

double mark1 = bundle.getDouble("number1");
double mark2 = bundle.getDouble("number2");

result1.setText(mark1 + "  ");
result2.setText(mark2 + "  ");

答案 1 :(得分:0)

错误是您使用相同的密钥“numbers”来添加附加内容,并且您正试图通过另一个密钥“number1”和“number2”来检索它们。你的代码应该是这样的:

Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
myIntent.putExtra("number1", marking1);
myIntent.putExtra("number2", marking2);

要检索它们,你应该使用:

Intent intent = getIntent();
double mark1 = intent.getDoubleExtra("number1", 0);
double mark2 = intent.getDoubleExtra("number2", 0);

然后,在EditTexts上设置变量,如下所示:

result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
result1.setText(mark1+"");
result2.setText(mark2+"");

答案 2 :(得分:0)

发送意图时,您使用相同的名称,尝试纠正它们。