Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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
无法解析方法';openFileOutput(java.lang.String,int)_Java_Android_Android Studio_Android File - Fatal编程技术网

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

无法解析方法';openFileOutput(java.lang.String,int),java,android,android-studio,android-file,Java,Android,Android Studio,Android File,前一天晚上,我在Android studio中实现了一个代码片段来访问一个文件并在文件中写入一个字符串。我不知道“openFileOutput()”方法有什么问题。 我已经尝试过在openFileOutput()方法之前添加上下文,但这不起作用,如果我没有错的话,它一定不会起作用 请帮帮我。下面是实现该代码段的代码部分 package com.medaino.www.twitterapp; import android.app.ListActivity; import android.cont

前一天晚上,我在Android studio中实现了一个代码片段来访问一个文件并在文件中写入一个字符串。我不知道“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);
    }


}
package.com.medaino.www.twitterapp;
导入android.app.ListActivity;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.ListView;
导入java.io.FileOutputStream;
导入java.io.ObjectOutputStream;
导入java.lang.String;
导入java.util.ArrayList;
导入java.util.List;
导入android.view.view;
导入android.content.Intent;
公共类tweetlistactivity扩展了ListActivity{
//私有列表视图tweetListView;
私有字符串[]字符串数组;
私人tweetaapter TweetItemArrayaAdapter;
公共列表getDataForListView()
{
List tweets=new ArrayList();
对于(int i=0;i<10;i++)
{
tweet twt=新tweet();
setTitle(“Tweet#“+i的一个好标题);
twt.setBody(“tweet的一些随机正文#”+i);
tweets.add(twt);
}
String FILENAME=“hello_file”;
String=“你好,世界!”;

FileOutputStream fos=Context.openFileOutput(文件名,模式\私有); fos.write(string.getBytes()); fos.close(); 返回推文; } @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u tweetlistactivity); stringArray=新字符串[10]; List tweetlist=getDataForListView(); tweetItemArrayAdapter=新的TweeteApter(this、stringArray、tweetlist); setListAdapter(tweetItemArrayAdapter); } @凌驾 受保护的void onListItemClick(列表视图列表、视图v、整数位置、长id) { 意向意向=新意向(此,tweet_detail.class); 星触觉(意向); } @凌驾 公共布尔onCreateOptions菜单(菜单) { //为菜单充气;这会将项目添加到操作栏(如果存在)。 getMenuInflater().充气(R.menu.menu\u tweetlistactivity,menu); 返回true; } @凌驾 公共布尔值onOptionsItemSelected(菜单项项){ //处理操作栏项目单击此处。操作栏将 //自动处理Home/Up按钮上的点击,只要 //在AndroidManifest.xml中指定父活动时。 int id=item.getItemId(); //noinspection SimplifiableIf语句 if(id==R.id.action\u设置){ 返回true; } 返回super.onOptionsItemSelected(项目); } }
openFileOutput();需要一个上下文。你在用ArrayAdapter调用它。应该是:

// Define your context then use below code
context.openFileOutput();
openFileOutput();需要一个上下文。你在用ArrayAdapter调用它。应该是:

// Define your context then use below code
context.openFileOutput();
openFileOutput();需要一个上下文。你在用ArrayAdapter调用它。应该是:

// Define your context then use below code
context.openFileOutput();
openFileOutput();需要一个上下文。你在用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();                                               

}   

它是这样工作的。当我删除本例中不需要的上下文时,我必须处理文件异常

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

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

     e.printStackTrace();                                               

}   

它是这样工作的。当我删除本例中不需要的上下文时,我必须处理文件异常

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

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

     e.printStackTrace();                                               

}   

它是这样工作的。当我删除本例中不需要的上下文时,我必须处理文件异常

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

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

     e.printStackTrace();                                               

}   


感谢您提醒我方法是在适配器内调用的。因此,我们设法改变了实现方式。但在这个实现中,我得到另一个错误,即不能从静态上下文引用非静态方法。请更新源代码。我来看看。但是如果上面的回答解决了您的第一个问题,请接受我的回答。FileOutputStream fos=Context.openFileOutput(FILENAME,MODE_PRIVATE);->FileOutputStream fos=openFileOutput(文件名,模式\私有);还是不行。我想我明白了。我将所有内容添加到try块并捕获异常。恭喜你!很高兴听到你通过了!感谢您提醒我方法是在适配器内调用的。因此,我们设法改变了实现方式。但在这个实现中,我得到另一个错误,即不能从静态上下文引用非静态方法。请更新源代码。我来看看。但是如果上面的回答解决了您的第一个问题,请接受我的回答。FileOutputStream fos=Context.openFileOutput(FILENAME,MODE_PRIVATE);->FileOutputStream fos=openFileOutput(文件名,模式\私有);还是不行。我想我明白了。我将所有内容添加到try块并捕获异常。恭喜你!很高兴听到你通过了!感谢您提醒我方法是在适配器内调用的。因此,我们设法改变了实现方式。但在这个实现中,我得到另一个错误,即不能从静态上下文引用非静态方法。请更新源代码。我来看看。但是如果上面的回答解决了您的第一个问题,请接受我的回答。FileOutputStream fos=Context.openFileOutput(FILENAME,MODE_PRIVATE);->FileOutputStream fos=openFileOutput(文件名,模式\私有);还是不行。我想我明白了。我将所有内容添加到try块并捕获异常。恭喜你!很高兴听到你通过了!感谢您提醒我方法是在适配器内调用的。因此,我们设法改变了实现方式。但在这个实现中,我得到了另一个错误,即不能从静态上下文引用非静态方法