[英]Cannot resolve method 'openFileOutput(java.lang.String,int)
A night before, I implemented a code snippet in Android studio to access a file and write a string in a file.But some how I am not able to build it sucessfully. 前一天晚上,我在Android Studio中实现了一个代码段来访问文件并在文件中写入字符串,但是有些方法使我无法成功构建它。 I don't know what is wrong with "openFileOutput()" method.
我不知道“ openFileOutput()”方法有什么问题。 I have already tried adding context before openFileOutput() method, but that doesn't work and if I am not wrong it must not.
我已经尝试过在openFileOutput()方法之前添加上下文,但这是行不通的,如果我没有记错的话,也一定不会。
Please help me. 请帮我。 Here is the code part implementing that snippet.
这是实现该代码段的代码部分。
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);
}
}
openFileOutput(); openFileOutput(); needs a Context.
需要一个上下文。 You are calling it in an ArrayAdapter.
您正在ArrayAdapter中调用它。 It should be:
它应该是:
// Define your context then use below code
context.openFileOutput();
IT WORKS THIS WAY. 这是可行的。 WHEN I REMOVE THE CONTEXT WHICH IS NOT REQUIRED IN THIS CASE, I HAVE TO HANDLE THE FILE EXCEPTION.
当我删除这种情况下不需要的上下文时,我必须处理文件例外。
try {
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);
fos.write(string.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.