意图中的可序列化对象以String形式返回

B_.*_*B_. 4 search android serializable android-intent

在我的应用程序中,我试图通过意图将可序列化对象传递给另一个活动.意图并非完全由我创建,它是通过搜索建议创建和传递的.

在搜索建议的内容提供程序中,创建对象并将其放置在SUGGEST_COLUMN_INTENT_EXTRA_DATAMatrixCursor 的列中.但是,在我调用的接收活动中getIntent().getSerializableExtra(SearchManager.EXTRA_DATA_KEY),返回的对象是String类型,我无法将其强制转换为原始对象类.

我尝试为我的对象制作一个parcelable包装器,调用out.writeSerializable(...)并使用它,但同样的事情发生了.

返回的字符串就像一个通用的Object toString(),即com.foo.yak.MyAwesomeClass@4350058,所以我假设toString()被调用到我无法控制的地方.

希望我只是遗漏了一些简单的东西.谢谢您的帮助!

编辑:我的一些代码

这是在充当搜索权限的内容提供者中:

//These are the search suggestion columns 
private static final String[] COLUMNS = {
    "_id",  // mandatory column
    SearchManager.SUGGEST_COLUMN_TEXT_1,
    SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
};

//This places the serializable or parcelable object (and other info) into the search suggestion
private Cursor getSuggestions(String query, String[] projection) {
    List<Widget> widgets = WidgetLoader.getMatches(query);

    MatrixCursor cursor = new MatrixCursor(COLUMNS);
    for (Widget w : widgets) {
        cursor.addRow(new Object[] {
                           w.id
                           w.name
                           w.data //This is the MyAwesomeClass object I'm trying to pass
                           });
    }

    return cursor;
}
Run Code Online (Sandbox Code Playgroud)

这是在接收搜索建议的活动中:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Object extra = getIntent().getSerializableExtra(SearchManager.EXTRA_DATA_KEY); 
    //extra.getClass() returns String, when it should return MyAwesomeClass, so this next line throws a ClassCastException and causes a crash
    MyAwesomeClass mac = (MyAwesomeClass)extra;
    ...
 }
Run Code Online (Sandbox Code Playgroud)

Ric*_*ler 6

阅读我对类似问题的回答.基本问题是MatrixCursor唯一适用于基类型并且依赖于AbstractCursor填充CursorWindow以在进程之间传递数据.AbstractCursor通过调用Object#toString每个行数据字段来完成此操作.换句话说,您不能通过a在进程之间传递任意对象MatrixCursor.