首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android java.lang.nullpointerexception读取文件

Android java.lang.nullpointerexception读取文件
EN

Stack Overflow用户
提问于 2014-03-08 08:32:50
回答 3查看 678关注 0票数 0

其他读取文件的方法?

为什么不读..?

读总是"java.lang.nullpointerexception".中的文本

代码语言:javascript
复制
public class TopRatedFragment extends Fragment {
private static final String FILENAME = "data.data";
private Context context;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.setting, container, false);
    EditText message = (EditText) rootView.findViewById(R.id.mesaage);
    read(rootView);
    return rootView;
}
   public void read(View view){
       EditText message = (EditText) view.findViewById(R.id.mesaage);
          try{
             FileInputStream fin = context.openFileInput(FILENAME);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                 temp = temp + Character.toString((char)c);
              }
                  //Read Text
             message.setText(temp, BufferType.EDITABLE);
          }catch(Exception e){
                  //read Eroor
              message.setText(e.toString(), BufferType.EDITABLE);
          }
    }


}

谁能帮我?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-08 08:54:19

给你一个简单的解决办法..。做一些事情,比如:

代码语言:javascript
复制
//First get your current context then use it...
context = getActivity();
FileInputStream fin = context.openFileInput(FILENAME);
票数 0
EN

Stack Overflow用户

发布于 2014-03-08 08:34:14

试试这个..。

使用EditText message作为全局不要像下面这样使用EditText message = (EditText) view.findViewById(R.id.mesaage);

代码语言:javascript
复制
EditText message;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.setting, container, false);
    message = (EditText) rootView.findViewById(R.id.mesaage);
    read(rootView);
    return rootView;
}
   public void read(View view){
          try{
             FileInputStream fin = context.openFileInput(FILENAME);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                 temp = temp + Character.toString((char)c);
              }
                  //Read Text
             message.setText(temp, BufferType.EDITABLE);
          }catch(Exception e){
                  //read Eroor
              message.setText(e.toString(), BufferType.EDITABLE);
          }
    }
票数 0
EN

Stack Overflow用户

发布于 2014-03-08 08:37:34

试着使用

代码语言:javascript
复制
FileInputStream fin = new FileInputStream(new File(FILENAME));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22267030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档