我很沮丧地将一个对象的arraylist发送给另一个活动。要发送的数据是通过asynctask(SplashScreen活动)中调用的webservice检索的。
我已经遵循了一些指导原则(即Problem unmarshalling parcelables),但没有任何运气。
事实上,第二个活动可以检索信息,并且应用程序正常运行,但异常会一直显示。
我真的很感激任何帮助,因为它让我永远地解决了这个问题。
例外:
01-14 11:31:26.490 2357-3205/? E/Parcel﹕ Class not found when unmarshalling: com.rebytes.ilistas.MapPacks
java.lang.ClassNotFoundException: com.rebytes.ilistas.MapPacks
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:204)
at java.lang.Class.forName(Class.java:169)
at android.os.Parcel.readParcelableCreator(Parcel.java:2091)
at android.os.Parcel.readParcelable(Parcel.java:2055)
at android.os.Parcel.readValue(Parcel.java:1971)
at android.os.Parcel.readMapInternal(Parcel.java:2255)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getString(Bundle.java:1082)
at android.content.Intent.getStringExtra(Intent.java:4961)
at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:3761)
at com.android.server.am.ActivityStack.startActivityMayWait(ActivityStack.java:4977)
at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3173)
at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3129)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:157)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2125)
at android.os.Binder.execTransact(Binder.java:388)
at dalvik.system.NativeStart.run(Native Method)
AsyncTask(SplashScreen活动)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
this.cont = getApplicationContext();
try {
String pack = getPackageManager().getPackageInfo(getPackageName(), 0).packageName;
toDownload = new MenuIntializationJSONFeedTask(this).execute("http://ilistas.slormin.com/v3/android/menuJSON.php").get();
Intent mainClass = new Intent(SplashScreen.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.setClassLoader(MainActivity.class.getClassLoader());
bundle.putParcelable("arraylist", toDownload);
mainClass.putExtras(bundle);
startActivity(mainClass);
finish();
overridePendingTransition(R.anim.appear, R.anim.disappear);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
private class MenuIntializationJSONFeedTask extends AsyncTask<String, Void, MapPacks> {
Context context;
public ArrayList<MenuItems> listaDeResultados = new ArrayList<MenuItems>();
private MenuIntializationJSONFeedTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected MapPacks doInBackground(String... params) {
JSONObject cadenaJSON = null;
try {
cadenaJSON = new JSONObject(readJSONFeed(params[0]));
JSONArray entradaMenu = cadenaJSON.getJSONArray("menu");
int i = 0;
while(i<entradaMenu.length()){
JSONObject cancion1 = entradaMenu.getJSONObject(i);
JSONObject detalleCancion = cancion1.getJSONObject("entradas");
com.rebytes.ilistas.MenuItems menuItem = new com.rebytes.ilistas.MenuItems(detalleCancion.getInt("Id"),
detalleCancion.getInt("Level"),
detalleCancion.getInt("Position"),
Base64.decode(detalleCancion.getString("Image"),0),
detalleCancion.getString("Name"),
detalleCancion.getString("Url"),
detalleCancion.getString("raizJSON"));
listaDeResultados.add(i,menuItem);
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
toDownload.setMappacks(listaDeResultados);
return toDownload;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(MapPacks result) {
super.onPostExecute(result);
}
}
public String readJSONFeed(String URL){
StringBuilder sB = new StringBuilder();
HttpClient hC = new DefaultHttpClient();
hC.getParams().setIntParameter("http.connection.timeout", 15000);
HttpGet hG = new HttpGet(URL);
try {
HttpResponse hR = hC.execute(hG);
StatusLine sL = hR.getStatusLine();
int statusCode = sL.getStatusCode();
if (statusCode == 200){
HttpEntity hE = hR.getEntity();
InputStream inputStream = hE.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine())!=null){
sB.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (IOException e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return sB.toString();
}
}
MapPacks(可分割的MenuItems对象的包装)
public class MapPacks implements Parcelable {
public ArrayList<MenuItems> mappacks;
public MapPacks(){}
protected MapPacks(Parcel in){
in.readBundle(MainActivity.class.getClassLoader());
mappacks = new ArrayList<MenuItems>();
//in.readList(mappacks,MainActivity.class.getClassLoader());
}
public void setMappacks(ArrayList<MenuItems> mappacks) {
this.mappacks = mappacks;
}
public List<MenuItems> getMappacks() {
return mappacks;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(mappacks);
}
public static final Parcelable.Creator<MapPacks> CREATOR
= new Parcelable.Creator<MapPacks>() {
@Override
public MapPacks createFromParcel(Parcel source) {
return new MapPacks(source);
}
@Override
public MapPacks[] newArray(int size) {
return new MapPacks[size];
}
};
}
MenuItems(由MapPacks包装的可分割对象)
public class MenuItems implements Parcelable{
public int id;
public int level;
public int idPadre;
public int position;
public byte[] image;
public String name;
public String url;
public String rootJson;
public MenuItems(int id, int level, int position, byte[] image, String name, String url, String rootJson) {
this.id = id;
this.level = level;
this.position = position;
this.image = image;
this.name = name;
this.url = url;
this.rootJson = rootJson;
}
public MenuItems() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getIdPadre() {
return idPadre;
}
public void setIdPadre(int idPadre) {
this.idPadre = idPadre;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getRootJson() {
return rootJson;
}
public void setRootJson(String rootJson) {
this.rootJson = rootJson;
}
public MenuItems(Parcel in) {
id = in.readInt();
level = in.readInt();
position = in.readInt();
image = in.createByteArray();
name = in.readString();
url = in.readString();
rootJson = in.readString();
}
public static final Parcelable.Creator<MenuItems> CREATOR = new Parcelable.Creator<MenuItems>() {
public MenuItems createFromParcel(Parcel in) {
MenuItems mp = new MenuItems();
mp.id = in.readInt();
mp.level = in.readInt();
mp.position = in.readInt();
mp.image = in.createByteArray();
mp.name = in.readString();
mp.url = in.readString();
mp.rootJson = in.readString();
return mp;
}
public MenuItems[] newArray(int size) {
MenuItems[] mps = new MenuItems[size];
for (int i = 0; i < size; ++i) {
mps[i] = new MenuItems();
}
return mps;
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(level);
dest.writeInt(position);
dest.writeByteArray(image);
dest.writeString(name);
dest.writeString(url);
dest.writeString(rootJson);
}
}