【发布时间】:2013-10-24 19:30:40
【问题描述】:
问题是当我将参数从对话框发送到 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();
}
}
这是主要活动(如下),在这里它给了我错误“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 编程有点新,所以请保留我的(:谢谢帮助...
【问题讨论】:
-
与您上次提出此问题时相同。请在
FileOutputStream stream = openFileOutput(filename, Context.MODE_PRIVATE);上设置断点调试您的应用程序。filename有什么价值?不知道这一点,就不可能帮助你。我还要重申,MainActivity mainActivity = new MainActivity()非常糟糕。如果您这样做,您的应用将永远无法正常运行。 -
PhotosData.txt 是否与 MainActivity 存在于同一文件夹中?
-
现在 MainActivity mainActivity = new MainActivity() 对我有好处,它只是 test 但在主要活动中:
FileOutputStream stream = openFileOutput(filename, Context.MODE_PRIVATE)它不起作用返回:public java.io.FileOutputStream openFileOutput(java.lang.String name, int mode) throws java.io.FileNotFoundException { /* compiled code */ } -
老实说。这是最好的方法 ;) 我曾两次问过你
filename的值。在我们知道这一点之前,我们无能为力。我投票结束这个问题。 -
save("PhotosData.txt", info) 在mainactivity中