Reputation: 1549
I took this code from Android Developers page but the code won't work. I've looked around but nobody seems to give a clear answer.
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();
}
}
The "openFileOutput" is apperently undefined. How do i write my code to make it write to the file and not fail to compile?
Here is my full code (if that helps)
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();
}
}
Upvotes: 0
Views: 544
Reputation: 1006839
The "openFileOutput" is apperently undefined. How do i write my code to make it write to the file and not fail to compile?
openFileOutput()
is a method on Context
and its subclasses.
Here is my full code (if that helps)
Inside a Fragment
, you can call getActivity()
to retrieve the Activity
that hosts the Fragment
. Activity
inherits from Context
, and so you can call openFileOutput()
on the Activity
.
Upvotes: 2