2015-06-19 108 views
0

前一天晚上,我在Android studio中實現了一個代碼片段來訪問一個文件並在文件中寫入一個字符串。但是有些我無法構建它成功了。我不知道「openFileOutput()」方法有什麼問題。 我已經嘗試在openFileOutput()方法之前添加上下文,但這不起作用,如果我沒有錯,它肯定不會。無法解析方法'openFileOutput(java.lang.String,int)

請幫幫我。以下是實現該代碼段的代碼部分。

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


} 

回答

1

openFileOutput();需要一個上下文。你在一個ArrayAdapter中調用它。它應該是:

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

謝謝你提醒我,該方法在適配器內被調用。所以只是設法改變了實現。但在這個實現中,我得到另一個錯誤,說非靜態方法不能從靜態上下文中引用。 –

+0

更新您的源代碼。我會看看。但如果它解決了您的第一個問題,請接受我上面的答案。 –

+0

我已經更新了代碼 –

0

是這樣工作的。當我移除這種情況下不需要的上下文時,我必須處理文件異常。

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

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

    e.printStackTrace();            

} 
相關問題