其他读取文件的方法?
为什么不读..?
读总是"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);
}
}
}谁能帮我?
发布于 2014-03-08 08:54:19
给你一个简单的解决办法..。做一些事情,比如:
//First get your current context then use it...
context = getActivity();
FileInputStream fin = context.openFileInput(FILENAME);发布于 2014-03-08 08:34:14
试试这个..。
使用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);
}
}发布于 2014-03-08 08:37:34
试着使用
FileInputStream fin = new FileInputStream(new File(FILENAME));https://stackoverflow.com/questions/22267030
复制相似问题