[英]OnActivityResult ()
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 如果删除
@Override
它会变得更好,但仍会显示错误消息
super.onActivityResult(requestCode, resultCode, data);
What is going wrong in here? 这里出了什么问题?
You cannot override onActivityResult
inside OnClickListener
because it does not exist in the base class. 您不能在
OnClickListener
覆盖onActivityResult
,因为它在基类中不存在。 Move your onActivityResult
code so that it is inside your Activity
class, not the OnClickListner
. 移动您的
onActivityResult
代码,使其位于Activity
类(而不是OnClickListner
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.