我正在开发一个Android新闻阅读器应用程序。我有一个实现Serializable的AsyncTask,所以我可以保存实例。我用它来旋转手机时处理应用程序刷新。它工作,当我旋转我的应用程序时,任务仍在运行。但是当任务运行时,我按下主页按钮/后退按钮关闭我的应用程序,我的应用程序将被强制关闭。我的代码有什么问题吗?
@Override
protected void onSaveInstanceState(final Bundle outState) {
// super.onSaveInstanceState(outState);
outState.putInt("curPage", mPager.getCurrentItem());
outState.putInt("drawerPos", drawerPos);
outState.putSerializable("task", task);
if (dialog != null) {
outState.putBoolean("isDialogShowing", dialog.isShowing());
} else {
outState.putBoolean("isDialogShowing", false);
}
// outState.putSerializable("dialog", dialog);
}
// dismiss dialog if activity is destroyed
@Override
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
if (task.getStatus() == AsyncTask.Status.RUNNING)
task.cancel(true);
task = null;
super.onDestroy();
}
MyAsyncTask:
public class AsyncTaskRefreshRss extends
AsyncTask<String, String, ArrayList<BeritaHeader>> implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3385729493967673390L;
private OnResponseListener responder;
// MyAlertDialogFragment myDialog;
ProgressDialog dialog;
FragmentManager fm;
Context context;
String type;
String subtype;
public AsyncTaskRefreshRss(Context context, FragmentManager fm,
String type, String subtype, OnResponseListener listener) {
this.fm = fm;
this.context = context;
this.type = type;
this.subtype = subtype;
this.responder = listener;
}
public void setFinishListener(OnResponseListener listener){
this.responder = listener;
}
public void setDialog(ProgressDialog dialog) {
this.dialog = dialog;
}
@Override
protected void onPreExecute() {
dialog.show();
}
@Override
protected ArrayList<BeritaHeader> doInBackground(String... arg0) {
MyWebService ws = new MyWebService();
try {
return ws.GetRSS(type, subtype);
} catch (Exception e) {
return null;
}
}
public interface OnResponseListener {
public void onSuccess();
public void onFailure();
}
@Override
protected void onPostExecute(ArrayList<BeritaHeader> result) {
if (dialog.isShowing())
dialog.dismiss();
if (result != null) {
NewsDataSource ds = new NewsDataSource(context);
ds.deleteAllBerita(type, subtype);
ds.createBerita(result, type, subtype);
responder.onSuccess();
} else {
Toast toast = Toast.makeText(context, "Gagal memuat",
Toast.LENGTH_LONG);
toast.show();
responder.onFailure();
}
// asyncState = DO_NOTHING;
}
public static class MyAlertDialogFragment extends DialogFragment {
public boolean isCanceled;
public static MyAlertDialogFragment newInstance() {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage("Sedang Memuat");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
isCanceled = true;
}
});
isCanceled = false;
return dialog;
}
}
}
我的logcat:
07-19 19:37:00.905: E/AndroidRuntime(29243): FATAL EXCEPTION: main
07-19 19:37:00.905: E/AndroidRuntime(29243): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.karyadigital.beritaku.berita.AsyncTaskRefreshRss)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Parcel.writeSerializable(Parcel.java:1279)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Parcel.writeValue(Parcel.java:1233)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Parcel.writeMapInternal(Parcel.java:591)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Bundle.writeToParcel(Bundle.java:1619)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Parcel.writeBundle(Parcel.java:605)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2247)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.app.ActivityThread$StopInfo.run(ActivityThread.java:2915)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Handler.handleCallback(Handler.java:615)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Looper.loop(Looper.java:137)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 19:37:00.905: E/AndroidRuntime(29243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-19 19:37:00.905: E/AndroidRuntime(29243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-19 19:37:00.905: E/AndroidRuntime(29243): at dalvik.system.NativeStart.main(Native Method)
07-19 19:37:00.905: E/AndroidRuntime(29243): Caused by: java.io.NotSerializableException: android.app.Application
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
07-19 19:37:00.905: E/AndroidRuntime(29243): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
07-19 19:37:00.905: E/AndroidRuntime(29243): at android.os.Parcel.writeSerializable(Parcel.java:1274)
07-19 19:37:00.905: E/AndroidRuntime(29243): ... 15 more
答案 0 :(得分:2)
您无法生成AsyncTask
可序列化并序列化它。原因:您无法制作,因为系统将尝试序列化其所有字段,依此类推每个字段的字段。如果某个字段不是Serializable且没有标记为transient
,那么您将获得Exception。
如果要在重新创建活动时将AsyncTask
与活动分离,请使用碎片配置更改。这是an article that describes how to。
答案 1 :(得分:0)
由于您的类中的Context字段,此操作失败。上下文对象不可序列化。
“遍历图形时,可能会遇到不支持Serializable接口的对象。在这种情况下,将抛出NotSerializableException并识别非可序列化对象的类。”
您可以完全删除Context字段,也可以将transient属性应用于Context字段,以便它不被序列化。
public class MyClass implements Serializable
{
...
public transient Context myContext;
...
}