我得到保存更改的按钮,并把它放在内部存储器中,然后当应用程序启动时我想读取文件,如果有任何ofc。我认为这将是应有的,但我是新鲜的编码器和dunno这是好的。
我不知道哪里出错了,这也是好的代码,请帮助我,因为我卡住了
我的代码:
公共类MyGameDetailsFragment扩展了Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_game_details, container, false);
try {
FileInputStream is = getActivity().openFileInput(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
} catch(OutOfMemoryError om) {
om.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
String result = sb.toString();
saveBtn = (Button) view.findViewById(R.id.savechangesbtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Data1 = s1.getText().toString();
Data2 = s2.getText().toString();
Data3 = s3.getText().toString();
Data4 = s4.getText().toString();
Data5 = s5.getText().toString();
Data6 = s6.getText().toString();
try {
FileOutputStream fos=getActivity().openFileOutput(file, getActivity().MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(fos);
outputStream.write(Data1.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data2.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data3.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data4.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data5.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data6.getBytes());
outputStream.close();
//Toast.makeText(context,"file saved",Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream inputStream = getActivity().openFileInput(file);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
} catch (Exception e) {
e.printStackTrace();
}
}
});
return view;
} }
Ps:s1,s2等在代码等中创建
权限
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
答案 0 :(得分:0)
首先我会建议你,不要在片段中用OnCreateView()方法编写代码,总是在OnViewCreated方法中编写代码。
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_game_details, container, false);
return view;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
try {
FileInputStream is = getActivity().openFileInput(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
} catch(OutOfMemoryError om) {
om.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
String result = sb.toString();
saveBtn = (Button) view.findViewById(R.id.savechangesbtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Data1 = s1.getText().toString();
Data2 = s2.getText().toString();
Data3 = s3.getText().toString();
Data4 = s4.getText().toString();
Data5 = s5.getText().toString();
Data6 = s6.getText().toString();
try {
FileOutputStream fos=getActivity().openFileOutput(file, getActivity().MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(fos);
outputStream.write(Data1.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data2.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data3.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data4.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data5.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data6.getBytes());
outputStream.close();
//Toast.makeText(context,"file saved",Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream inputStream = getActivity().openFileInput(file);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
如果您遇到任何错误,请写下错误的堆栈跟踪。
由于
答案 1 :(得分:0)
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Other = (TextView) view.findViewById(R.id.Details);
OtherD = (TextView) view.findViewById(R.id.OtherDetails);
try {
FileInputStream is = getActivity().openFileInput(file);
byte[] input = new byte[is.available()];
while(is.read(input) != -1){
value += new String(input);
}
is.getFD().sync();
//is.flush();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String[] strArray = value.split(";");
s1.setText(strArray[1]);
s2.setText(strArray[2]);
s3.setText(strArray[3]);
s4.setText(strArray[4]);
s5.setText(strArray[5]);
s6.setText(strArray[6]);
s7.setText(strArray[7]);
saveBtn = (Button) view.findViewById(R.id.savechangesbtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Data1 = s1.getText().toString();
Data2 = s2.getText().toString();
Data3 = s3.getText().toString();
Data4 = s4.getText().toString();
Data5 = s5.getText().toString();
Data6 = s6.getText().toString();
Data7= s7.getText().toString();
try {
FileOutputStream fos=getActivity().openFileOutput(file, getActivity().MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(fos);
outputStream.write(string3.getBytes());
outputStream.write(Data1.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data2.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data3.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data4.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data5.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data6.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data7.getBytes());
outputStream.write(string3.getBytes());
//outputStream.getFD().sync();
outputStream.flush();
outputStream.close();
//Toast.makeText(context,"file saved",Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream inputStream = getActivity().openFileInput(file);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
自己解决