0

我正在尝试将两个数字输入从一个活动传输到另一个活动的 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 + "  ");

    }

}
4

3 回答 3

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 + "  ");
于 2013-09-27T15:43:57.860 回答
0

您在发送意图时使用相同的名称作为附加信息,请尝试更正它们。

于 2013-09-27T16:02:09.990 回答
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+"");
于 2013-09-27T15:49:39.953 回答