如何从另一个类调用方法?

时间:2017-11-05 03:04:12

标签: java android

我有2节课,我把我的方法放在头等舱

First.class

public void countIN (View view) {
    counter++;
    if (counter == 3){
        if (mInterstitialAd.isLoaded()){
            mInterstitialAd.show();
        }
    }
}

我想在我的第一堂课中调用我的方法

Second.class

First myMethod = new First();
........
private void onClickButton(final ExpandableLayout expandableLayout) {
    expandableLayout.toggle();
    myMethod.countIN();
}

但它不起作用,我收到此错误:StackTrace

Error:(85, 17) error: method countIN in class RecyclerViewActivity cannot be applied to given types;
required: View
found: no arguments
reason: actual and formal argument lists differ in length

2 个答案:

答案 0 :(得分:1)

你的方法 countIN(View view)需要一个(1)参数,这是一个View类型。但是,你没有通过调用它传递任何东西 myMethod.countIN(); 要么删除方法定义中的参数,(因为你没有使用它,因为我可以看到) 或者,您可以在方法调用中提供参数。

答案 1 :(得分:0)

您可以在调用方法

时将null作为参数传递
myMethod.countIN(null);

但这仅用于测试和进一步操作,您必须停止将View视图作为方法中的参数:

public void countIN () {
counter++;
if (counter == 3){
    if (mInterstitialAd.isLoaded()){
        mInterstitialAd.show();
    }
}

或使用参数

public void countIN (View view) {
counter++;
if (counter == 3){
    if (mInterstitialAd.isLoaded()){
        mInterstitialAd.show();
        view.(do something)
    }
}