【问题标题】:Constructor Undefined Error - Java构造函数未定义错误 - Java
【发布时间】:2018-01-04 12:21:02
【问题描述】:

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

主文件:

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

它抛出构造函数未定义错误。 任何帮助将不胜感激。

【问题讨论】:

  • 你的测试类中如何定义tag和output_file?
  • 请提供minimal reproducible example。您的第一个 sn-p 代码由于各种原因无法编译,我们不知道 TAGOUTPUT_FILE 的类型,您也没有包含完整的错误文本。
  • Context context - AndroidTestCase 是否分别扩展。实现Context?
  • 附注Public class Test应该是public class Test

标签: java android constructor


【解决方案1】:

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

new TestUtils(this, TAG, OUTPUT_FILE);

你没有传递Context对象

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

结帐this问题

【讨论】:

    【解决方案2】:

    你在做

    TestUtils(this, TAG, OUTPUT_FILE);
    

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

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

    【讨论】:

    • 可能有助于解释签名的工作原理。但这确实回答了这个问题。
    • @ΦXocę 웃 Пepeúpa ツ 这就是问题所在。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 2010-11-14
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多