0

2 つの数値入力を 1 つのアクティビティから別の UI に転送しようとしていますが、ボタンをクリックしてインテントを変更するとクラッシュします。

logcat で次のように表示されます: http://pastebin.com/zkWPcSNZ。これは、データを CalcResult の editTexts に解析する方法に問題があることを示唆しています。

私の質問は、データを CalcResult editText に渡そうとしている方法の何が問題なのかということです。これを達成する別の方法はありますか?

私の 2 つのクラスは、参照用に次のようになります。

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

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 に答える