Getting "Expected 2 arguments but found 1" when getting the intExtra on the Activity result. Not sure what's wrong here

huangapple go评论75阅读模式
英文:

Getting "Expected 2 arguments but found 1" when getting the intExtra on the Activity result. Not sure what's wrong here

问题

我正在完成谷歌的“Android基础知识代码实验室”的作业练习,链接在这里 -

https://codelabs.developers.google.com/codelabs/android-training-create-an-activity/index.html?index=..%2F..android-training#11

但我一直遇到这个错误:

预期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 ->

https://codelabs.developers.google.com/codelabs/android-training-create-an-activity/index.html?index=..%2F..android-training#11

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); 

huangapple
  • 本文由 发表于 2020年9月13日 21:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63871435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定