[英]How to solve Function1 (Context) in Function1 cannot be applied to ()
I have the following problems: 我有以下问题:
I would like to call a function from another class so I added this line of code 我想从另一个类调用一个函数,所以我添加了这一行代码
Function1 func = new Function1();
and I get an error saying 我收到一个错误消息
Function1 (Context) in Function1 cannot be applied to () Function1中的Function1(上下文)不能应用于()
Furthermore, relating to this function and its error, I intend calling the aforementioned function which takes a JSON object and a Filename as parameters and it returns a file, however, when I enter it, I get the following error 此外,关于此函数及其错误,我打算调用上述函数,该函数以JSON对象和Filename作为参数,并返回一个文件,但是,当我输入它时,出现以下错误
Wrong 2nd argument type, found Java.lang.String required Java.io.File
The code in question is this: 有问题的代码是这样的:
JSONObject export = jsonArray1.getJSONObject(index);
File file = func.exportToFile(export, "Export.json");
The fuction in question starts like this: 有问题的功能是这样开始的:
public void exportToFile(JSONObject objectToExport, File fN)
{
String output = objectToExport.toString();
file_ = fN;
if (!file_.exists()) {
try {
file_.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
FileOutputStream fOut = new FileOutputStream(file_);
fOut.write(output.getBytes());
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
NB: I have tried to call the function like this: 注意:我试图这样调用该函数:
File file = func.exportToFile(export, func.file); 文件文件= func.exportToFile(export,func.file);
but I only get the error saying incompatible types 但我只收到错误消息,指出类型不兼容
Required Java.io.file 必需的Java.io.file
Found Void 发现虚空
What have I done wrong? 我做错了什么?
this func.exportToFile(export, func.file);
这个
func.exportToFile(export, func.file);
will not return anything since exportToFile
it's a void method . 由于
exportToFile
是空方法,因此不会返回任何内容。
change your method to make it return file this way : 更改您的方法,使其以这种方式返回文件:
public File exportToFile(JSONObject objectToExport, File fN) {
String output = objectToExport.toString();
file_ = fN;
if (!file_.exists()) {
try {
file_.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
FileOutputStream fOut = new FileOutputStream(file_);
fOut.write(output.getBytes());
fOut.close();
return file_;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.