Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有没有办法在非活动类中使用openFileOutput()方法?_Android_File Io_Android Activity - Fatal编程技术网

Android 有没有办法在非活动类中使用openFileOutput()方法?

Android 有没有办法在非活动类中使用openFileOutput()方法?,android,file-io,android-activity,Android,File Io,Android Activity,有没有办法在非活动类中使用openFileOutput()方法 我有一个私有类扩展了AsyncTask,我想在onPostExecute()中执行该方法,但我不能这样做,因为我的IDE显示以下错误: 类型的方法openFileOutput(String,int)未定义 FunkcjeAPI.Logowanie 什么是替代方案?我试图使用getActivity().openFileOutput(),但后来我看到了相同的错误 有什么建议吗 我通过添加到我的AsyncTaskContext对象的构造函

有没有办法在非活动类中使用
openFileOutput()
方法

我有一个私有类扩展了
AsyncTask
,我想在
onPostExecute()
中执行该方法,但我不能这样做,因为我的IDE显示以下错误:

类型的方法openFileOutput(String,int)未定义 FunkcjeAPI.Logowanie

什么是替代方案?我试图使用
getActivity().openFileOutput()
,但后来我看到了相同的错误


有什么建议吗

我通过添加到我的
AsyncTask
Context
对象的构造函数并键入以下内容,解决了我的问题:

context.openFileOutput();

我通过向我的
AsyncTask
Context
对象的构造函数中添加并键入以下内容来解决问题:

context.openFileOutput();

为AsyncTask创建一个构造函数并在上下文中传递,以便以后使用

private class MyTask extends AsyncTask<Void, Void, Long> {

    private Context context;

    public MyTask (Context context) {
        this.context = context;
    }

    // other code...

    @Override
    protected void onPostExecute(Long result) {
        // use context here
    }
}

为AsyncTask创建一个构造函数并在上下文中传递,以便以后使用

private class MyTask extends AsyncTask<Void, Void, Long> {

    private Context context;

    public MyTask (Context context) {
        this.context = context;
    }

    // other code...

    @Override
    protected void onPostExecute(Long result) {
        // use context here
    }
}

Android中没有
openFileStream()
方法。是的,有:该方法未命名为
openFileStream()
。你可以通过阅读来判断。我改了,还是一样的错误……然后你输入了错误的内容
openFileOutput()
Context
上的一个方法,
Activity
继承自
Context
,并且
getActivity()
返回一个
Activity
。Android中没有
openFileStream()
方法。是的,有:该方法未命名为
openFileStream()
。你可以通过阅读来判断。我改了,还是一样的错误……然后你输入了错误的内容
openFileOutput()
Context
上的一个方法,
Activity
继承自
Context
,并且
getActivity()
返回一个
Activity