构造函数未定义错误 - Java

时间:2017-07-28 12:18:40

标签: java android constructor

尝试从另一个类中调用一个方法,该方法存在于不同的文件中。

主档案:

public class Test extends AndroidTestCase {
    mTestUtils = new TestUtils(this, TAG, OUTPUT_FILE);

第二档:

public class TestUtils {
    public TestUtils(Context context, String tag, String outputFile) {

        mContext = context;
        mTag = tag;
        mOutputFile = outputFile;
    }
}

它抛出Constructor未定义的错误。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:5)

你正在做什么

TestUtils(this, TAG, OUTPUT_FILE);

this在这种情况下不是上下文。

方法getContext()将为您提供,只需查看doc

即可

答案 1 :(得分:0)

TestUtils构造函数的签名采用Context类型的对象,当您在Test类中执行此操作时

new TestUtils(this, TAG, OUTPUT_FILE);

你没有传递Context对象

而不是this您应该传递Context对象

结帐this问题

相关问题