其他阅读文件的方法。??
为什么不读..?
读取中的文本始终为“ java.lang.nullpointerexception ”。
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);
}
}
}
谁可以帮助我。??
答案 0 :(得分:0)
试试这个..
使用EditText message
作为全局不要使用EditText message = (EditText) view.findViewById(R.id.mesaage);
,如下所示
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);
}
}
答案 1 :(得分:0)
尝试使用
FileInputStream fin = new FileInputStream(new File(FILENAME));
答案 2 :(得分:0)
只是一个简单的解决方案.. 做一些事情:
//First get your current context then use it...
context = getActivity();
FileInputStream fin = context.openFileInput(FILENAME);