开发者

getFilesDir() android function is undefined if i dont extend my class with activity

开发者 https://www.devze.com 2023-02-19 00:21 出处:网络
If i have my class as import java.io.File; import java.io.FileOutputStream; //Extends Activity public class MyClass extends Activity

If i have my class as

import java.io.File;

import java.io.FileOutputStream;

//Extends Activity

public class MyClass extends Activity
{

File fileDir = getFilesDir(); //no error

   . ....    
}

But if i don't extend MyClass , i get an error saying "getFilesDir()" undefined.

some thing like

   public class MyClass2
    {

    ....

    File fileDir = getFilesDir(); 开发者_运维问答//error

    }


That is the method of the Context class. You have to obtain an instance of the class to invoke it.


http://developer.android.com/reference/android/content/ContextWrapper.html#getFilesDir()

getFilesDir is member of ContextWrapper. So if you don't extend ContextWrapper it's normal for the compiler to throw error undefined reference.

0

精彩评论

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