开发者

android shortcut, can shortcut intent extra contain application objects?

开发者 https://www.devze.com 2023-02-06 20:59 出处:网络
i have an app that registers for CREATE_SHORTCUT, and sets up an intent for the shortcut that targets one of my activities. as an extra. the intent contains one of my application objects ... that is,

i have an app that registers for CREATE_SHORTCUT, and sets up an intent for the shortcut that targets one of my activities. as an extra. the intent contains one of my application objects ... that is, an object of a type defined in my application.

when i add the shortcut, i get the exception pasted in below. my guess here is that the problem is that the home screen app needs to deserialize that intent extra, and it has no visibility to that class. that would imply that any app extras for a shortcut intent must be standard Android SDK objects.

anything i'm missing?

E/AndroidRuntime(  141): Caused by: java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = org.jtb.nextvta.Route)
E/AndroidRuntime(  141):        at android.os.Parcel.readSerializable(Parcel.java:1890)
E/AndroidRuntime(  141):        at android.os.Parcel.readValue(Parcel.java:1761)
E/AndroidRuntime(  141):        at android.os.Parcel.readMapInternal(Parcel.java:1947)
E/AndroidRuntime(  141):        at android.os.Bundle.unparcel(Bundle.java:169)
E/AndroidRuntime(  141):        at android.os.Bundle.keySet(Bundle.java:256)
E/AndroidRuntime(  141):        at android.content.Intent.toUri(Intent.java:4868)
E/AndroidRuntime(  141):        at com.android.launcher.ApplicationInfo.onAddToDatabase(ApplicationInfo.java:103)
E/AndroidRuntime(  141):        at com.android.launcher.LauncherModel.addItemToDatabase(LauncherModel.java:1386)
E/AndroidRuntime(  141):        at com.android.launcher.Launcher.addShortcut(Launcher.java:740)
E/AndroidRuntime(  141):        at com.android.launcher.Launcher.completeAddShortcut(Launcher.java:670)
E/AndroidRuntime(  141):        at com.android.launcher.Launcher.onActivityResult(Launcher.java:378)
E/AndroidRuntime(  141):        at android开发者_Go百科.app.Activity.dispatchActivityResult(Activity.java:3625)
E/AndroidRuntime(  141):        at android.app.ActivityThread.deliverResults(ActivityThread.java:3220)
E/AndroidRuntime(  141):        ... 11 more
E/AndroidRuntime(  141): Caused by: java.lang.ClassNotFoundException: org.jtb.nextvta.Route
E/AndroidRuntime(  141):        at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(  141):        at java.lang.Class.forName(Class.java:237)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2595)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1848)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:852)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2006)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:956)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2289)
E/AndroidRuntime(  141):        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2243)
E/AndroidRuntime(  141):        at android.os.Parcel.readSerializable(Parcel.java:1884)
E/AndroidRuntime(  141):        ... 23 more
E/AndroidRuntime(  141): Caused by: java.lang.NoClassDefFoundError: org.jtb.nextvta.Route
E/AndroidRuntime(  141):        ... 33 more
E/AndroidRuntime(  141): Caused by: java.lang.ClassNotFoundException: org.jtb.nextvta.Route in loader dalvik.system.PathClassLoader@4376dac0
E/AndroidRuntime(  141):        at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
E/AndroidRuntime(  141):        at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
E/AndroidRuntime(  141):        at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
E/AndroidRuntime(  141):        ... 33 more


Sounds about right. The launcher tries to use the intent and hits the java.lang.ClassNotFoundException exception from the custom class. There's no way (that I know of) to "add" the classes to the launcher "classpath".

Couple of ideas:

Try "serializing" your application object into a string (or a byte array). It doesn't have to be official Java serialization, it can be a simple string format that you can convert to/from an application instance. Then store the string in the intent and handle the string intent on the receiving activity.

Alternatively, store the application object's instance variables individually in the intent (assuming the ivars are primitives and/or standard objects like String).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号