getFilesDir() 在子类中查看

发布于 2024-12-15 10:53:50 字数 662 浏览 4 评论 0原文

我可以在使用 extends View 的 .class 中使用 getFilesDir() 方法吗?

public void save(){
    try {
        File myDir = new File(getFilesDir().getAbsolutePath());
        FileWriter fw = new FileWriter(myDir + "/Test.txt");
        fw.write(Integer.toString(score));
        fw.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

只是方法 getFilesDir() 收到错误消息,并提供一个快速修复方法 - 创建方法 getFilesDir()。这是因为它位于一个带有 extends View 的类中,我可以通过将该方法放入 Activity 类中并使用 static 来使用 View 类中的方法来解决此问题。这不起作用,程序崩溃并且分数无法保存。

Can I use the method getFilesDir() in a .class that use extends View?

public void save(){
    try {
        File myDir = new File(getFilesDir().getAbsolutePath());
        FileWriter fw = new FileWriter(myDir + "/Test.txt");
        fw.write(Integer.toString(score));
        fw.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

just the method getFilesDir() gets a error message with one quick fix available - create method getFilesDir(). And this is because it's in a class with extends View, I can fix this by putting the method in an Activity class and use the method in the View class by using static. This don't work ether, the program crash and the score doesn't get saved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2024-12-22 10:53:50

只需将 Context 传递给您的 View 构造函数,您就可以获得 SD 卡的位置。

[编辑]
我的意思是,你的这个扩展 View 的类可能不存在于任何可以本地访问上下文的地方,因此当你在 Activity 中实例化它时,你需要在构造函数 FROM 中传递该对象(你确实有对其的引用)。

所以像这样......

class Foo extends View{
   public foo (Context c){
      c.getFilesDir(); // or any method that belongs to context
   }
}

然后在你的活动中

class Main extends Activity{
   @override
   public void onCreate(){
       super.onCreate();


       Foo foo = new Foo(this); //this is a reference to the activity AND is also the context;
   }
}

Just pass the Context in to your View constructor and you'll be able to get the sdcard location.

[Edit]
What I mean is that this class of yours that extends View probably doesn't exist anywhere where it has access to the context natively so you need to pass it that object in the constructor FROM when you instantiate it in your Activity (where you DO have a reference to it).

so something like this...

class Foo extends View{
   public foo (Context c){
      c.getFilesDir(); // or any method that belongs to context
   }
}

then in your Activity

class Main extends Activity{
   @override
   public void onCreate(){
       super.onCreate();


       Foo foo = new Foo(this); //this is a reference to the activity AND is also the context;
   }
}
み青杉依旧 2024-12-22 10:53:50

使用 getContext 方法

 getContext().getFilesDir();

use the getContext method

 getContext().getFilesDir();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文