このコードはAndroid Developers ページから取得しましたが、コードは機能しません。私は周りを見回しましたが、誰も明確な答えを出していないようです。
private void writetofile(String FILENAME, String content){
FileOutputStream outputStream;
try {
//The problem is here ↓
outputStream = openFileOutput(FILENAME, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
「openFileOutput」は明らかに未定義です。コードをファイルに書き込み、コンパイルに失敗しないようにするにはどうすればよいですか?
これが私の完全なコードです(それが役立つ場合)
public class HastighetFragment extends Fragment {
int color;
Button bAdd;
private void writetofile(String FILENAME, String content){
FileOutputStream outputStream;
try {
outputStream = openFileOutput(FILENAME, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hastighet, container, false);
bAdd = (Button) rootView.findViewById(R.id.addbutton);
color=0;
bAdd.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
switch(color){
case 0:
bAdd.setBackgroundResource(R.drawable.buttonstylered);
color=1;
break;
case 1:
bAdd.setBackgroundResource(R.drawable.buttonstylegreen);
color=0;
break;
}
}});
return rootView;
}
public void onBackPressed() {
android.app.FragmentManager FragmentManager = getActivity().getFragmentManager();
FragmentManager.popBackStack();
}
}