开发者

Refer to non static method from static method

I am trying to call the following function from a s开发者_开发百科tatic method.

File directory = getDir(folderName, Context.MODE_PRIVATE);

Any suggestions on how to fix Cannot make a static reference to the non-static method getDir(String, int) from the type ContextWrapper


Without knowing more about your situation or method, I'd say you'll need to pass a Context into your static method and call getDir() on that Object.


Just make your getFiles func public static like this:

public static ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i=0; i<files.length; i++)
            MyFiles.add(files[i].getName());
    }

    return MyFiles;
}

And you'll be able to access it from another activity.


Do

File fileName = New File();
File directory = fileName.getDir(folderName, Context.MODE_PRIVATE);

Also look here for a better explanation of what you were doing wrong.


This is the error which comes , when we are using "getConTentResolver()" in static method like:

 public static void Mthd()
 {
   Cursor cursor =getContentResolver().query(uri, null, null, null, null);
   //ur next code
  }

So, in this case it will give an error, Therefore we have to make the function non-static.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜