0

我无法将对象存储到设备存储中。起初我得到 aNotSerializableException所以我使对象 Serializable 并且现在在try {} catch {}块中它正在抛出StreamCorruptedException我认为指向的行in = new ObjectInputStream(byteArray);。结果,该列表没有被填充。我可以在块之外填充列表,try {} catch {}但这会破坏将列表对象存储到设备上的目的。以下是相关代码:

public class MainListFragment extends ListFragment implements Serializable {

    OnListSelectedListener mCallback;
    public transient ObjectStorage mainObjectList = new ObjectStorage(); // creates
                                                                        // the
                                                                        // list
                                                                        // of
                                                                        // objects
    SharedPreferences mPrefs;
    int mCurrentPosition = -1;

    // The container Activity must implement this interface so the frag can
    // deliver messages
    public interface OnListSelectedListener {

        /** Called by ListFragment when a list item is selected */
        public void onItemSelected(int position, String schedulename,
                String[] ampm, boolean[] days, int[] times, boolean vibrate);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();

        updateStorage();
        ByteArrayInputStream byteArray = new ByteArrayInputStream(mPrefs
                .getString("myobject", "").getBytes());
        ObjectInputStream in;
        try {
            in = new ObjectInputStream(byteArray);
            ObjectStorage updatedStorageList = (ObjectStorage) in.readObject();
            Log.i("mydebug", "Populating list with storage");
            CustomListAdapter adapter = new CustomListAdapter(getActivity(),
                    R.layout.listview_item_row, updatedStorageList);
            // setListAdapter(new ArrayAdapter<String>(getActivity(), layout,
            // arraylist));
            setListAdapter(adapter);
            setListShown(true);
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void updateStorage() {
        getActivity();// used for MODE_PRIVATE

        // store object list into android system
        mPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = mPrefs.edit();
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream out;
        try {
            Log.i("mydebug", "Updating object storage");
            out = new ObjectOutputStream(arrayOutputStream);
            out.writeObject(mainObjectList);
            out.close();
            arrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ed.putString("myobject", arrayOutputStream.toString());
        ed.commit();
    }
}

以及 LogCat 信息:

03-02 00:41:07.857: I/mydebug(12435): TEMPMAINFRAG: MainListFragment{41dfe598 id=0x7f070028}
03-02 00:41:07.857: I/mydebug(12435): 1
03-02 00:41:07.857: D/AbsListView(12435): Get MotionRecognitionManager
03-02 00:41:07.877: D/AbsListView(12435): Get MotionRecognitionManager
03-02 00:41:07.877: I/mydebug(12435): Updating object storage
03-02 00:41:07.877: W/System.err(12435): java.io.StreamCorruptedException
03-02 00:41:07.877: W/System.err(12435):    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2106)
03-02 00:41:07.877: W/System.err(12435):    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
03-02 00:41:07.877: W/System.err(12435):    at com.example.android.fragments.MainListFragment.onStart(MainListFragment.java:94)
03-02 00:41:07.877: W/System.err(12435):    at android.support.v4.app.Fragment.performStart(Fragment.java:1332)
03-02 00:41:07.877: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
03-02 00:41:07.877: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
03-02 00:41:07.877: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
03-02 00:41:07.877: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1815)
03-02 00:41:07.877: W/System.err(12435):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:518)
03-02 00:41:07.877: W/System.err(12435):    at com.example.android.fragments.MainActivity.onStart(MainActivity.java:104)
03-02 00:41:07.877: W/System.err(12435):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
03-02 00:41:07.887: W/System.err(12435):    at android.app.Activity.performStart(Activity.java:5058)
03-02 00:41:07.887: W/System.err(12435):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-02 00:41:07.887: W/System.err(12435):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
03-02 00:41:07.887: W/System.err(12435):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3554)
03-02 00:41:07.887: W/System.err(12435):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-02 00:41:07.887: W/System.err(12435):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1230)
03-02 00:41:07.887: W/System.err(12435):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 00:41:07.887: W/System.err(12435):    at android.os.Looper.loop(Looper.java:137)
03-02 00:41:07.887: W/System.err(12435):    at android.app.ActivityThread.main(ActivityThread.java:4918)
03-02 00:41:07.887: W/System.err(12435):    at java.lang.reflect.Method.invokeNative(Native Method)
03-02 00:41:07.887: W/System.err(12435):    at java.lang.reflect.Method.invoke(Method.java:511)
03-02 00:41:07.887: W/System.err(12435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-02 00:41:07.887: W/System.err(12435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-02 00:41:07.887: W/System.err(12435):    at dalvik.system.NativeStart.main(Native Method)
03-02 00:41:07.947: I/Adreno200-EGLSUB(12435): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-02 00:41:08.908: I/mydebug(12435): TEMPMAINFRAG: MainListFragment{41e237f8 id=0x7f070028}
03-02 00:41:08.908: I/mydebug(12435): 1
03-02 00:41:08.918: D/AbsListView(12435): Get MotionRecognitionManager
03-02 00:41:08.928: D/AbsListView(12435): Get MotionRecognitionManager
03-02 00:41:08.938: I/mydebug(12435): Updating object storage
03-02 00:41:08.938: W/System.err(12435): java.io.StreamCorruptedException
03-02 00:41:08.938: W/System.err(12435):    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2106)
03-02 00:41:08.938: W/System.err(12435):    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
03-02 00:41:08.938: W/System.err(12435):    at com.example.android.fragments.MainListFragment.onStart(MainListFragment.java:94)
03-02 00:41:08.938: W/System.err(12435):    at android.support.v4.app.Fragment.performStart(Fragment.java:1332)
03-02 00:41:08.938: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
03-02 00:41:08.938: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
03-02 00:41:08.938: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
03-02 00:41:08.948: W/System.err(12435):    at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1815)
03-02 00:41:08.948: W/System.err(12435):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:518)
03-02 00:41:08.948: W/System.err(12435):    at com.example.android.fragments.MainActivity.onStart(MainActivity.java:104)
03-02 00:41:08.948: W/System.err(12435):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
03-02 00:41:08.948: W/System.err(12435):    at android.app.Activity.performStart(Activity.java:5058)
03-02 00:41:08.948: W/System.err(12435):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-02 00:41:08.948: W/System.err(12435):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
03-02 00:41:08.948: W/System.err(12435):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3554)
03-02 00:41:08.948: W/System.err(12435):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-02 00:41:08.948: W/System.err(12435):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1230)
03-02 00:41:08.948: W/System.err(12435):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 00:41:08.948: W/System.err(12435):    at android.os.Looper.loop(Looper.java:137)
03-02 00:41:08.958: W/System.err(12435):    at android.app.ActivityThread.main(ActivityThread.java:4918)
03-02 00:41:08.958: W/System.err(12435):    at java.lang.reflect.Method.invokeNative(Native Method)
03-02 00:41:08.958: W/System.err(12435):    at java.lang.reflect.Method.invoke(Method.java:511)
03-02 00:41:08.958: W/System.err(12435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-02 00:41:08.958: W/System.err(12435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-02 00:41:08.958: W/System.err(12435):    at dalvik.system.NativeStart.main(Native Method)
03-02 00:41:09.178: I/Adreno200-EGLSUB(12435): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-02 00:41:52.326: W/IInputConnectionWrapper(12435): getExtractedText on inactive InputConnection
03-02 00:41:52.326: W/IInputConnectionWrapper(12435): getTextBeforeCursor on inactive InputConnection
03-02 00:41:52.326: W/IInputConnectionWrapper(12435): getSelectedText on inactive InputConnection
03-02 00:41:52.326: W/IInputConnectionWrapper(12435): getTextAfterCursor on inactive InputConnection
03-02 00:41:52.326: W/IInputConnectionWrapper(12435): getExtractedText on inactive InputConnection
03-02 00:41:52.326: W/IInputConnectionWrapper(12435): getTextBeforeCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getExtractedText on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getTextBeforeCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getSelectedText on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getTextAfterCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getExtractedText on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getTextBeforeCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getSelectedText on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getTextAfterCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): beginBatchEdit on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): endBatchEdit on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getExtractedText on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getTextBeforeCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getSelectedText on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): getTextAfterCursor on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): beginBatchEdit on inactive InputConnection
03-02 00:41:52.336: W/IInputConnectionWrapper(12435): endBatchEdit on inactive InputConnection
4

1 回答 1

0

摆脱所有 ByteArrayInput/OutputStreams,转换为 String,String.getBytes() 调用,以及所有其他不是直接调用 writeObject() 的对象,直接连接到 FileOutputStream,或者相反从直接连接到 FileInputStream 的 ObjectInputStream 中直接读取所需的对象。

您正在三个或四个不同的点损坏数据。即使它没有破坏数据,也没有实际需要任何这种恶意。

于 2013-03-02T23:01:27.860 回答