問題は、ダイアログから mainActivity にパラメーターを送信するときです。
次の方法でパラメーターを送信しています。
mainActivity.SaveInfo(info);
mainActivity に移動します。私のコードは以下の通りです:
RateDialog.java コード:
public class RateDialog extends DialogFragment {
public String path;
public float rate;
public boolean i;
PhotosRatingInfo photosRatingInfo;
PhotosList photosList;
public RateDialog(String path, float rate, PhotosList photosList) {
this.path = path;
this.rate = rate;
this.photosList = photosList;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog = inflater.inflate(R.layout.dialog_rate, null);
final RatingBar ratingBar = (RatingBar)dialog.findViewById(R.id.ratingBar);
/** Dialog builder and settings **/
builder.setView(dialog)
.
setTitle("Rate this photo")
.
setPositiveButton("Rate",new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog,int id){
rate = ratingBar.getRating();
Logic.PhotosRatingInfo info = new Logic.PhotosRatingInfo();
MainActivity mainActivity = new MainActivity();
info.setRate(rate);
info.setPhotopath(path);
photosList.list.add(info);
***mainActivity.SaveInfo(info);***
}
}
)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
);
return builder.create();
}
}
これは mainactivity (以下) であり、ここでエラー「"public java.io.FileOutputStream openFileOutput(java.lang.String name, int mode) throws java.io.FileNotFoundException " on:
FileOutputStream stream = openFileOutput(filename, Context.MODE_PRIVATE);`
MainActivity.java のコード:
public class MainActivity extends Activity {
public void SaveInfo(Logic.PhotosRatingInfo info)
{
save("PhotosData.txt", info)
}
private void save(String filename, Object data) {
try {
FileOutputStream stream = openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(stream);
os.writeObject(data);
//stream.write(filename.getBytes());
os.close();
stream.close();
} catch(Exception e) {
}
}
}
私はJavaでのプログラミングに少し慣れていないので、私のものにしてください(:助けてくれてありがとう...