OnActivityResult()

时间:2013-02-09 00:57:44

标签: java android methods

我的代码应该允许我从计算器中获取值并进一步使用它:

//-----------------This section creates the keypad functionality
for (int o = 0; o < keybuttons.length; o++) {
    final int n = o;
    keybuttons[o] = (Button) findViewById(data.keyIds[n]);
    keybuttons[o].setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                String tmp = texts[selectEdit].getText()
                    .toString();
                switch (n) {
                case 3:
                    texts[selectEdit].setText(tmp.substring(0, tmp.length() - 1));
                    break; //get cursor position and delete char
                case 7:
                    {
                        // Create intent for RealCalc.
                        Intent intent2 = new Intent("uk.co.quarticsoftware.REALCALC");
                        double x = 0; // Set initial value (double).
                        if (!texts[selectEdit].getText()
                            .toString()
                            .equals("")) {
                            x = Double.valueOf(texts[selectEdit].getText()
                                .toString());
                        }
                        intent2.putExtra("X", x);
                        // Launch calculator
                        try {
                            startActivityForResult(intent2, 0);
                        } catch (ActivityNotFoundException e) {
                            Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("market://details?id=uk.co.nickfines.RealCalc"));
                            try {
                                startActivity(intent);
                            } catch (ActivityNotFoundException f) {
                                // Google Play Store app not available.
                            }
                        }
                        break;
                    } //open Calculator
                case 11:
                    {
                        if (!tmp.contains("E")) texts[selectEdit].setText(tmp + "" + keybuttons[n].getText());
                        break;
                    } //check for E if dont have do default case
                case 15:
                    {
                        TL.setVisibility(View.GONE);
                        break;
                    } //simulate back button
                default:
                    {
                        texts[selectEdit].setText(tmp + "" + keybuttons[n].getText());
                        //get cursor start and end and get entire String
                        // replace selected String with button text
                        //insert back
                        break;
                    }
                } //end of switch
            } //end of try
            catch (ActivityNotFoundException e) {
                Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("market://details?id=uk.co.nickfines.RealCalc"));
                // Calculator not installed
            } //calculator.num=n;
            catch (Exception e) {
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                EasyPhysActivity.error = sw.toString();
            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                // User pressed OK.
                double value = data.getDoubleExtra("X", Double.NaN);
                if (Double.isNaN(value)) {
                    // Calculation result was "Error".
                } else {
                    // Calculation result ok.
                }
            } else {
                // User pressed cancel or back button.
            }
        }
    });

}
//----------------------------------------

但它不喜欢这三行:

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

如果我删除@Override它会变得更好,但它仍会显示错误

super.onActivityResult(requestCode, resultCode, data);

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

您无法覆盖onActivityResult内的OnClickListener,因为它在基类中不存在。移动您的onActivityResult代码,使其位于Activity课程内,而不是OnClickListner