【问题标题】:Cannot resolve method 'openFileOutput(java.lang.String,int)无法解析方法'openFileOutput(java.lang.String,int)
【发布时间】:2015-06-19 04:21:16
【问题描述】:

前一天晚上,我在 Android Studio 中实现了一个代码 sn-p 来访问文件并在文件中写入一个字符串。但是有些我无法成功构建它。我不知道“openFileOutput()”方法有什么问题。 我已经尝试在 openFileOutput() 方法之前添加上下文,但这不起作用,如果我没记错的话,一定不会。

请帮助我。这是实现该 sn-p 的代码部分。

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);
    }


}

【问题讨论】:

    标签: java android android-studio android-file


    【解决方案1】:

    openFileOutput();需要一个上下文。您在 ArrayAdapter 中调用它。应该是:

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

    【讨论】:

    • 感谢您提醒我该方法是在适配器内部调用的。所以只是设法改变了实施。但在这个实现中,我得到另一个错误,说不能从静态上下文中引用非静态方法。
    • 更新您的源代码。我会看看。但是,如果它解决了您的第一个问题,请接受我上面的回答。
    • FileOutputStream fos = Context.openFileOutput(FILENAME,MODE_PRIVATE); -> FileOutputStream fos = openFileOutput(FILENAME,MODE_PRIVATE);
    • 还是不行。我想我明白了。我将所有内容添加到 try 块并捕获异常。然后就可以了
    • 恭喜!很高兴听到你通过了!
    【解决方案2】:

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

    try {                                                                  
      String FILENAME = "hello_file";                                       
      String string = "hello world!";                                       
    
      FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);        
      fos.write(string.getBytes());                                         
     } catch (Exception e) {                                                
    
         e.printStackTrace();                                               
    
    }   
    

    【讨论】:

      猜你喜欢
      • 2015-03-30
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 2019-03-30
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多