4

当我开始一项活动时,我的应用程序中发生了异常,并且这种类型的错误显示在 logcat 中,所以请您帮助我。我放在那里的这三门课你能指导我吗?此代码当时使用错误锥 context.startActivity(new Intent(context, AnswerActivity.class)); 当时这条线的使用发生了错误

threadid=1: thread exiting with uncaught exception (group=0x2aacc8a0)
        FATAL EXCEPTION: main
        java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.jaapp.treeview.InMemoryTreeStateManager)
        at android.os.Parcel.writeSerializable(Parcel.java:1160)
        at android.os.Parcel.writeValue(Parcel.java:1114)
        at android.os.Parcel.writeMapInternal(Parcel.java:479)
        at android.os.Bundle.writeToParcel(Bundle.java:1552)
        at android.os.Parcel.writeBundle(Parcel.java:493)
        at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1574)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3334)
        at android.app.ActivityThread.access$2500(ActivityThread.java:132)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2078)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4669)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.io.NotSerializableException: com.jaapp.dto.QuestionsDto
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
        at java.util.HashMap.writeObject(HashMap.java:1025)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1229)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
        at android.os.Parcel.writeSerializable(Parcel.java:1155)

代码:

    class SimpleStandardAdapter extends AbstractTreeViewAdapter<QuestionsDto> {
        public String[] Colorname = { "#FFFF00", "#ACCC00", "#FF5D00" };
        Context context;

        WebView descriptionView;

        public SimpleStandardAdapter(final QuestionsActivity treeViewListDemo,
                final Set<String> selected,
                final TreeStateManager<QuestionsDto> treeStateManager,
                final int numberOfLevels) {
            super(treeViewListDemo, treeStateManager, numberOfLevels);
            context = treeViewListDemo;

        }

        private String getDescription(final QuestionsDto id) {
            final Integer[] hierarchy = getManager().getHierarchyDescription(id);
            Log.i("log_tag", "" + id.getQid());
            return id.getQquestion();
            // return "Node " + id + Arrays.asList(hierarchy);
        }

        @Override
        public View getNewChildView(final TreeNodeInfo<QuestionsDto> treeNodeInfo) {
            final LinearLayout viewLayout = (LinearLayout) getActivity()
                    .getLayoutInflater().inflate(R.layout.demo_list_item, null);
            return updateView(viewLayout, treeNodeInfo);
        }

        @Override
        public LinearLayout updateView(final View view,
                final TreeNodeInfo<QuestionsDto> treeNodeInfo) {
            Log.v("log_tag", "get item id:" + treeNodeInfo.getId());
            final LinearLayout viewLayout = (LinearLayout) view;
            /*
             * descriptionView = (WebView) viewLayout
             * .findViewById(R.id.demo_list_item_description);
             */

            final TextView descriptionView = (TextView) viewLayout
                    .findViewById(R.id.demo_list_item_description);

            descriptionView.setText(
                    Html.fromHtml(getDescription(treeNodeInfo.getId())),
                    TextView.BufferType.SPANNABLE);
            /*
             * descriptionView.loadData(getDescription(treeNodeInfo.getId()),
             * "text/html", "utf-8");
             */

            /*
             * Spanned marked_up = Html.fromHtml(myString);
             * textview.setText(marked_up.toString(),BufferType.SPANNABLE);
             */

            view.setBackgroundColor(Color.parseColor("#FFFF00"));
            descriptionView.setBackgroundColor(Color.parseColor("#00000000"));

            // levelView.setText(Integer.toString(treeNodeInfo.getLevel()));
            /*
             * final CheckBox box = (CheckBox) viewLayout
             * .findViewById(R.id.demo_list_checkbox);
             */
            // box.setTag(treeNodeInfo.getId());
            /*
             * if (treeNodeInfo.isWithChildren()) { box.setVisibility(View.GONE); }
             * else { box.setVisibility(View.VISIBLE);
             * box.setChecked(selected.contains(treeNodeInfo.getId())); }
             * box.setOnCheckedChangeListener(onCheckedChange);
             */

            /*
             * descriptionView.seton(new OnClickListener() {
             * 
             * @Override public void onClick(View v) { // TODO Auto-generated method
             * stub Log.i("log_tag", "Click" + treeNodeInfo.getId()); } });
             */
            return viewLayout;
        }

        @Override
        public void handleItemClick(final View view, final Object id) {

            final QuestionsDto longId = (QuestionsDto) id;
            final TreeNodeInfo<QuestionsDto> info = getManager()
                    .getNodeInfo(longId);

            if (info.isWithChildren()) {
                super.handleItemClick(view, id);
                Log.i("log_tag", "Click" + info.getId());

            } else {
                final ViewGroup vg = (ViewGroup) view;
                /*
                 * final CheckBox cb = (CheckBox) vg
                 * .findViewById(R.id.demo_list_checkbox); cb.performClick();
                 */

            }
            Log.i("log_tag", "Click Event Call ID::::: " + info.getId().getQid());
            Jaappdb.getanswer(info.getId().getQid());

            /*
             * Intent intent = new Intent(context, AnswerActivity.class);
             * intent.putExtra("key_QuestionId", info.getId().getQid());
             * context.startActivity(intent);
             */
            context.startActivity(new Intent(context, AnswerActivity.class));

        }

        @Override
        public long getItemId(final int position) {
            return position;
        }

    }
     ====================================================


    package com.example.treelistviewdemo.treeview;
    /**
     * In-memory manager of tree state.
     * 
     * @param <T>
     *            type of identifier
     */
    public class InMemoryTreeStateManager<T> implements TreeStateManager<T> {
        private static final String TAG = InMemoryTreeStateManager.class
                .getSimpleName();
        private static final long serialVersionUID = 1L;
        private final Map<T, InMemoryTreeNode<T>> allNodes = new HashMap<T, InMemoryTreeNode<T>>();
        private final InMemoryTreeNode<T> topSentinel = new InMemoryTreeNode<T>(
                null, null, -1, true);
        private transient List<T> visibleListCache = null; // lasy initialised
        private transient List<T> unmodifiableVisibleList = null;
        private boolean visibleByDefault = true;
        private final transient Set<DataSetObserver> observers = new HashSet<DataSetObserver>();

        private synchronized void internalDataSetChanged() {
            visibleListCache = null;
            unmodifiableVisibleList = null;
            for (final DataSetObserver observer : observers) {
                observer.onChanged();
            }
        }

        /**
         * If true new nodes are visible by default.
         * 
         * @param visibleByDefault
         *            if true, then newly added nodes are expanded by default
         */
        public void setVisibleByDefault(final boolean visibleByDefault) {
            this.visibleByDefault = visibleByDefault;
        }

        private InMemoryTreeNode<T> getNodeFromTreeOrThrow(final T id) {
            if (id == null) {
                throw new NodeNotInTreeException("(null)");
            }
            final InMemoryTreeNode<T> node = allNodes.get(id);
            if (node == null) {
                throw new NodeNotInTreeException(id.toString());
            }
            return node;
        }

        private InMemoryTreeNode<T> getNodeFromTreeOrThrowAllowRoot(final T id) {
            if (id == null) {
                return topSentinel;
            }
            return getNodeFromTreeOrThrow(id);
        }

        private void expectNodeNotInTreeYet(final T id) {
            final InMemoryTreeNode<T> node = allNodes.get(id);
            if (node != null) {
                throw new NodeAlreadyInTreeException(id.toString(), node.toString());
            }
        }

        @Override
        public synchronized TreeNodeInfo<T> getNodeInfo(final T id) {
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrow(id);
            final List<InMemoryTreeNode<T>> children = node.getChildren();
            boolean expanded = false;
            if (!children.isEmpty() && children.get(0).isVisible()) {
                expanded = true;
            }
            return new TreeNodeInfo<T>(id, node.getLevel(), !children.isEmpty(),
                    node.isVisible(), expanded);
        }

        @Override
        public synchronized List<T> getChildren(final T id) {
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            return node.getChildIdList();
        }

        @Override
        public synchronized T getParent(final T id) {
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            return node.getParent();
        }

        private boolean getChildrenVisibility(final InMemoryTreeNode<T> node) {
            boolean visibility;
            final List<InMemoryTreeNode<T>> children = node.getChildren();
            if (children.isEmpty()) {
                visibility = visibleByDefault;
            } else {
                visibility = children.get(0).isVisible();
            }
            return visibility;
        }

        @Override
        public synchronized void addBeforeChild(final T parent, final T newChild,
                final T beforeChild) {
            expectNodeNotInTreeYet(newChild);
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(parent);
            final boolean visibility = getChildrenVisibility(node);
            // top nodes are always expanded.
            if (beforeChild == null) {
                final InMemoryTreeNode<T> added = node.add(0, newChild, visibility);
                allNodes.put(newChild, added);
            } else {
                final int index = node.indexOf(beforeChild);
                final InMemoryTreeNode<T> added = node.add(index == -1 ? 0 : index,
                        newChild, visibility);
                allNodes.put(newChild, added);
            }
            if (visibility) {
                internalDataSetChanged();
            }
        }

        @Override
        public synchronized void addAfterChild(final T parent, final T newChild,
                final T afterChild) {
            expectNodeNotInTreeYet(newChild);
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(parent);
            final boolean visibility = getChildrenVisibility(node);
            if (afterChild == null) {
                final InMemoryTreeNode<T> added = node.add(
                        node.getChildrenListSize(), newChild, visibility);
                allNodes.put(newChild, added);
            } else {
                final int index = node.indexOf(afterChild);
                final InMemoryTreeNode<T> added = node.add(
                        index == -1 ? node.getChildrenListSize() : index + 1, newChild,
                        visibility);
                allNodes.put(newChild, added);
            }
            if (visibility) {
                internalDataSetChanged();
            }
        }

        @Override
        public synchronized void removeNodeRecursively(final T id) {
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            final boolean visibleNodeChanged = removeNodeRecursively(node);
            final T parent = node.getParent();
            final InMemoryTreeNode<T> parentNode = getNodeFromTreeOrThrowAllowRoot(parent);
            parentNode.removeChild(id);
            if (visibleNodeChanged) {
                internalDataSetChanged();
            }
        }

        private boolean removeNodeRecursively(final InMemoryTreeNode<T> node) {
            boolean visibleNodeChanged = false;
            for (final InMemoryTreeNode<T> child : node.getChildren()) {
                if (removeNodeRecursively(child)) {
                    visibleNodeChanged = true;
                }
            }
            node.clearChildren();
            if (node.getId() != null) {
                allNodes.remove(node.getId());
                if (node.isVisible()) {
                    visibleNodeChanged = true;
                }
            }
            return visibleNodeChanged;
        }

        private void setChildrenVisibility(final InMemoryTreeNode<T> node,
                final boolean visible, final boolean recursive) {
            for (final InMemoryTreeNode<T> child : node.getChildren()) {
                child.setVisible(visible);
                if (recursive) {
                    setChildrenVisibility(child, visible, true);
                }
            }
        }

        @Override
        public synchronized void expandDirectChildren(final T id) {
            Log.d(TAG, "Expanding direct children of " + id);
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            setChildrenVisibility(node, true, false);
            internalDataSetChanged();
        }

        @Override
        public synchronized void expandEverythingBelow(final T id) {
            Log.d(TAG, "Expanding all children below " + id);
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            setChildrenVisibility(node, true, true);
            internalDataSetChanged();
        }

        @Override
        public synchronized void collapseChildren(final T id) {
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            if (node == topSentinel) {
                for (final InMemoryTreeNode<T> n : topSentinel.getChildren()) {
                    setChildrenVisibility(n, false, true);
                }
            } else {
                setChildrenVisibility(node, false, true);
            }
            internalDataSetChanged();
        }

        @Override
        public synchronized T getNextSibling(final T id) {
            final T parent = getParent(id);
            final InMemoryTreeNode<T> parentNode = getNodeFromTreeOrThrowAllowRoot(parent);
            boolean returnNext = false;
            for (final InMemoryTreeNode<T> child : parentNode.getChildren()) {
                if (returnNext) {
                    return child.getId();
                }
                if (child.getId().equals(id)) {
                    returnNext = true;
                }
            }
            return null;
        }

        @Override
        public synchronized T getPreviousSibling(final T id) {
            final T parent = getParent(id);
            final InMemoryTreeNode<T> parentNode = getNodeFromTreeOrThrowAllowRoot(parent);
            final T previousSibling = null;
            for (final InMemoryTreeNode<T> child : parentNode.getChildren()) {
                if (child.getId().equals(id)) {
                    return previousSibling;
                }
            }
            return null;
        }

        @Override
        public synchronized boolean isInTree(final T id) {
            return allNodes.containsKey(id);
        }

        @Override
        public synchronized int getVisibleCount() {
            return getVisibleList().size();
        }

        @Override
        public synchronized List<T> getVisibleList() {
            T currentId = null;
            if (visibleListCache == null) {
                visibleListCache = new ArrayList<T>(allNodes.size());
                do {
                    currentId = getNextVisible(currentId);
                    if (currentId == null) {
                        break;
                    } else {
                        visibleListCache.add(currentId);
                    }
                } while (true);
            }
            if (unmodifiableVisibleList == null) {
                unmodifiableVisibleList = Collections
                        .unmodifiableList(visibleListCache);
            }
            return unmodifiableVisibleList;
        }

        public synchronized T getNextVisible(final T id) {
            final InMemoryTreeNode<T> node = getNodeFromTreeOrThrowAllowRoot(id);
            if (!node.isVisible()) {
                return null;
            }
            final List<InMemoryTreeNode<T>> children = node.getChildren();
            if (!children.isEmpty()) {
                final InMemoryTreeNode<T> firstChild = children.get(0);
                if (firstChild.isVisible()) {
                    return firstChild.getId();
                }
            }
            final T sibl = getNextSibling(id);
            if (sibl != null) {
                return sibl;
            }
            T parent = node.getParent();
            do {
                if (parent == null) {
                    return null;
                }
                final T parentSibling = getNextSibling(parent);
                if (parentSibling != null) {
                    return parentSibling;
                }
                parent = getNodeFromTreeOrThrow(parent).getParent();
            } while (true);
        }

        @Override
        public synchronized void registerDataSetObserver(
                final DataSetObserver observer) {
            observers.add(observer);
        }

        @Override
        public synchronized void unregisterDataSetObserver(
                final DataSetObserver observer) {
            observers.remove(observer);
        }

        @Override
        public int getLevel(final T id) {
            return getNodeFromTreeOrThrow(id).getLevel();
        }

        @Override
        public Integer[] getHierarchyDescription(final T id) {
            final int level = getLevel(id);
            final Integer[] hierarchy = new Integer[level + 1];
            int currentLevel = level;
            T currentId = id;
            T parent = getParent(currentId);
            while (currentLevel >= 0) {
                hierarchy[currentLevel--] = getChildren(parent).indexOf(currentId);
                currentId = parent;
                parent = getParent(parent);
            }
            return hierarchy;
        }

        private void appendToSb(final StringBuilder sb, final T id) {
            if (id != null) {
                final TreeNodeInfo<T> node = getNodeInfo(id);
                final int indent = node.getLevel() * 4;
                final char[] indentString = new char[indent];
                Arrays.fill(indentString, ' ');
                sb.append(indentString);
                sb.append(node.toString());
                sb.append(Arrays.asList(getHierarchyDescription(id)).toString());
                sb.append("\n");
            }
            final List<T> children = getChildren(id);
            for (final T child : children) {
                appendToSb(sb, child);
            }
        }

        @Override
        public synchronized String toString() {
            final StringBuilder sb = new StringBuilder();
            appendToSb(sb, null);
            return sb.toString();
        }

        @Override
        public synchronized void clear() {
            allNodes.clear();
            topSentinel.clearChildren();
            internalDataSetChanged();
        }

        @Override
        public void refresh() {
            internalDataSetChanged();
        }

    }


    ======================================================================================================
    package com.example.treelistviewdemo.treeview;
    import java.io.Serializable;
    import java.util.List;
    import android.database.DataSetObserver;
    /**
     * Manages information about state of the tree. It only keeps information about
     * tree elements, not the elements themselves.
     * 
     * @param <T>
     *            type of the identifier for nodes in the tree
     */
    public interface TreeStateManager<T> extends Serializable {

        /**
         * Returns array of integers showing the location of the node in hierarchy.
         * It corresponds to heading numbering. {0,0,0} in 3 level node is the first
         * node {0,0,1} is second leaf (assuming that there are two leaves in first
         * subnode of the first node).
         * 
         * @param id
         *            id of the node
         * @return textual description of the hierarchy in tree for the node.
         */
        Integer[] getHierarchyDescription(T id);

        /**
         * Returns level of the node.
         * 
         * @param id
         *            id of the node
         * @return level in the tree
         */
        int getLevel(T id);

        /**
         * Returns information about the node.
         * 
         * @param id
         *            node id
         * @return node info
         */
        TreeNodeInfo<T> getNodeInfo(T id);

        /**
         * Returns children of the node.
         * 
         * @param id
         *            id of the node or null if asking for top nodes
         * @return children of the node
         */
        List<T> getChildren(T id);

        /**
         * Returns parent of the node.
         * 
         * @param id
         *            id of the node
         * @return parent id or null if no parent
         */
        T getParent(T id);

        /**
         * Adds the node before child or at the beginning.
         * 
         * @param parent
         *            id of the parent node. If null - adds at the top level
         * @param newChild
         *            new child to add if null - adds at the beginning.
         * @param beforeChild
         *            child before which to add the new child
         */
        void addBeforeChild(T parent, T newChild, T beforeChild);

        /**
         * Adds the node after child or at the end.
         * 
         * @param parent
         *            id of the parent node. If null - adds at the top level.
         * @param newChild
         *            new child to add. If null - adds at the end.
         * @param afterChild
         *            child after which to add the new child
         */
        void addAfterChild(T parent, T newChild, T afterChild);

        /**
         * Removes the node and all children from the tree.
         * 
         * @param id
         *            id of the node to remove or null if all nodes are to be
         *            removed.
         */
        void removeNodeRecursively(T id);

        /**
         * Expands all children of the node.
         * 
         * @param id
         *            node which children should be expanded. cannot be null (top
         *            nodes are always expanded!).
         */
        void expandDirectChildren(T id);

        /**
         * Expands everything below the node specified. Might be null - then expands
         * all.
         * 
         * @param id
         *            node which children should be expanded or null if all nodes
         *            are to be expanded.
         */
        void expandEverythingBelow(T id);

        /**
         * Collapse children.
         * 
         * @param id
         *            id collapses everything below node specified. If null,
         *            collapses everything but top-level nodes.
         */
        void collapseChildren(T id);

        /**
         * Returns next sibling of the node (or null if no further sibling).
         * 
         * @param id
         *            node id
         * @return the sibling (or null if no next)
         */
        T getNextSibling(T id);

        /**
         * Returns previous sibling of the node (or null if no previous sibling).
         * 
         * @param id
         *            node id
         * @return the sibling (or null if no previous)
         */
        T getPreviousSibling(T id);

        /**
         * Checks if given node is already in tree.
         * 
         * @param id
         *            id of the node
         * @return true if node is already in tree.
         */
        boolean isInTree(T id);

        /**
         * Count visible elements.
         * 
         * @return number of currently visible elements.
         */
        int getVisibleCount();

        /**
         * Returns visible node list.
         * 
         * @return return the list of all visible nodes in the right sequence
         */
        List<T> getVisibleList();

        /**
         * Registers observers with the manager.
         * 
         * @param observer
         *            observer
         */
        void registerDataSetObserver(final DataSetObserver observer);

        /**
         * Unregisters observers with the manager.
         * 
         * @param observer
         *            observer
         */
        void unregisterDataSetObserver(final DataSetObserver observer);

        /**
         * Cleans tree stored in manager. After this operation the tree is empty.
         * 
         */
        void clear();

        /**
         * Refreshes views connected to the manager.
         */
        void refresh();
    }
4

4 回答 4

19

你的问题的根本原因是

Caused by: java.io.NotSerializableException: com.jaapp.dto.QuestionsDto

这清楚地表明您的类不可序列化。确保你的类实现了 Serializable 标记接口。

于 2012-11-21T12:25:08.480 回答
3

解决了我的问题

MainActv extends AppCompatActivity implements Serializable, Parcelable{

....
//

    @Override
    public int describeContents() {return 0;}

    @Override
    public void writeToParcel(Parcel dest, int flags) {}

}
于 2017-05-20T04:47:59.403 回答
2

错误是编写类的可序列化对象,请确保:

  1. 类 QuestionsDto 已经实现了 Serializable 接口。
  2. 而且,您已经定义了写入和读取对象的方法。

请通过链接查看应如何定义可打包或可序列化的类。

于 2012-11-21T12:22:56.003 回答
2

简单地说,所有子类和内部子类都必须像这样实现 Serializable。

public class ActivationResponse implements Serializable, Parcelable {}
于 2018-11-09T06:50:46.193 回答