2017-07-28 80 views
1

我试图调用另一个类中存在于不同文件中的方法。构造函数未定义的错误 - Java

主文件:

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

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

+0

如何在测试类中定义tag和output_file? –

+0

请提供[mcve]。你的第一部分代码不会为*各种原因编译,我们不知道'TAG'或'OUTPUT_FILE'类型,也没有包含完整的错误文本。 –

+2

'上下文上下文' - 是否'AndroidTestCase'扩展resp。实现'Context'? –

回答

5

你正在做

TestUtils(this, TAG, OUTPUT_FILE); 

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

方法getContext()将提供你,只是看的doc

+0

可能有助于解释签名如何工作。但这确实回答了这个问题。 –

+0

@ΦXocę웃Пepeúpaツ这是问题。谢谢。 – taz

+0

@taz欢迎您! –

0

TestUtils构造方法的签名需要Context类型的对象,当你这样做的Test

new TestUtils(this, TAG, OUTPUT_FILE); 

你不及格Context对象

而不是this您应该通过Context对象

结帐this问题