本文介绍了的onActivityResult()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有code应该让我从计算器采取价值,进一步使用它:
I have code which should allow me to take value from calculator and use it further:
//-----------------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.
}
}
});
}
//----------------------------------------
不过,这并不喜欢这些三行:
But it doesn't like these three lines:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
如果我删除 @覆盖
变得更好,但它仍然显示了一个错误
If I delete @Override
it becomes better, but it still shows an error for
super.onActivityResult(requestCode, resultCode, data);
这是怎么回事错在这里?
What is going wrong in here?
推荐答案
您不能覆盖的onActivityResult
在 OnClickListener
,因为它不会在基类存在。移动你的的onActivityResult
code,以便它是你的活动里面
类,而不是 OnClickListner
。
You cannot override onActivityResult
inside OnClickListener
because it does not exist in the base class. Move your onActivityResult
code so that it is inside your Activity
class, not the OnClickListner
.
这篇关于的onActivityResult()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!