英文:
Getting "Expected 2 arguments but found 1" when getting the intExtra on the Activity result. Not sure what's wrong here
问题
我正在完成谷歌的“Android基础知识代码实验室”的作业练习,链接在这里 -
但我一直遇到这个错误:
预期2个参数,但找到1个
当我尝试在Activity结果方法中获取intExtra
时出现此错误。我将在下面贴出代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
count = data.getIntExtra(HelloActivity.EXTRA_REPLY);
mTextView.setText(String.valueOf(count));
}
}
}
错误出现在第40行(count = data.getIntExtra(HelloActivity.EXTRA_REPLY);
)。
我知道这里还有其他类似的问题,但我在Android和Java方面是一个初学者,所以很难理解那些解决方案,以对我的问题有用的方式。一个通俗易懂的解释也会非常受欢迎。谢谢!
英文:
I am doing the homework exercise from googles "Android fundamentals codelab" here ->
and I keep getting this:
Expected 2 arguments but found 1
error when I try to get the intExtra
on the Activity result method. I'll post the code below
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
count = data.getIntExtra(HelloActivity.EXTRA_REPLY);
mTextView.setText(String.valueOf(count));
}
}
}
The error occurs on line 40 (count = data.getIntExtra(HelloActivity.EXTRA_REPLY);
).
I know there are other questions here that ask something similar but I am a beginner on Android and java in general so it is difficult for me to understand those solutions in a way that is useful for my problem. a Layman's terms explanation would also be greatly appreciated. thank you!
答案1
得分: 1
你正在使用的方法需要两个参数:
getIntExtra(String name, int defaultValue)
因此,只需添加第二个int参数,指定默认值,以防找不到“name”,就像这样:
int defaultValue = -1;
count = data.getIntExtra(HelloActivity.EXTRA_REPLY, defaultValue);
英文:
The method you are using requires 2 parameters:
getIntExtra(String name, int defaultValue)
So, just add a second int parameter, specifying the default value, in case the name
is not found, something like this:
int defaultValue = -1;
count = data.getIntExtra(HelloActivity.EXTRA_REPLY, defaultValue);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论