无法解析方法 'openFileOutput(java.lang.String,int)

Cannot resolve method 'openFileOutput(java.lang.String,int)

前一天晚上,我在 Android studio 中实现了一个代码片段来访问一个文件并在 file.But 中写入一个字符串,但我无法成功构建它。我不知道 "openFileOutput()" 方法有什么问题。 我已经尝试在 openFileOutput() 方法之前添加上下文,但这行不通,如果我没记错,那一定不行。

请帮帮我。这是实现该片段的代码部分。

package com.medaino.www.twitterapp;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;


import android.view.View;
import android.content.Intent;


public class tweetlistactivity extends ListActivity {
   // private ListView tweetListView;
    private String[] stringArray ;
    private tweetaapter tweetItemArrayAdapter;



    public List<tweet> getDataForListView()
    {
        List<tweet> tweets = new ArrayList<tweet>();
        for ( int i = 0; i < 10; i++ )
        {
            tweet twt = new tweet();
            twt.setTitle("A nice header for Tweet # " +i);
            twt.setBody("Some random body text for the tweet # " +i);
            tweets.add(twt);

        }

        String FILENAME = "hello_file";
        String string = "hello world!";

        FileOutputStream fos = Context.openFileOutput(FILENAME,MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();

        return tweets;
    }





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweetlistactivity);
        stringArray = new String[10];
        List<tweet> tweetlist = getDataForListView() ;
        tweetItemArrayAdapter = new tweetaapter(this, stringArray, tweetlist);
        setListAdapter(tweetItemArrayAdapter);

    }
    @Override
    protected void onListItemClick(ListView list, View v, int position, long id)
    {
        Intent intent = new Intent(this, tweet_detail.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_tweetlistactivity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

打开文件输出();需要一个上下文。您在 ArrayAdapter 中调用它。应该是:

// Define your context then use below code
context.openFileOutput();

它是这样工作的。当我删除在这种情况下不需要的上下文时,我必须处理文件异常。

try {                                                                  
  String FILENAME = "hello_file";                                       
  String string = "hello world!";                                       

  FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);        
  fos.write(string.getBytes());                                         
 } catch (Exception e) {                                                

     e.printStackTrace();                                               

}