Question

I have a problem with my aplication. I have a class that I put the values correctly and works fine, I think, but when I try to get the values of my class that I have on a bundle, I have this error:

04-16 02:59:12.900: E/AndroidRuntime(1443): FATAL EXCEPTION: main
04-16 02:59:12.900: E/AndroidRuntime(1443): Process: com.example.en4dis, PID: 1443
04-16 02:59:12.900: E/AndroidRuntime(1443): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.en4dis/com.example.en4dis.activitySelectEnvironment}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.en4dis.Point[]
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.os.Looper.loop(Looper.java:137)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.ActivityThread.main(ActivityThread.java:4998)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at java.lang.reflect.Method.invoke(Method.java:515)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at dalvik.system.NativeStart.main(Native Method)
04-16 02:59:12.900: E/AndroidRuntime(1443): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.en4dis.Point[]
04-16 02:59:12.900: E/AndroidRuntime(1443):     at com.example.en4dis.activitySelectEnvironment.onCreate(activitySelectEnvironment.java:108)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.Activity.performCreate(Activity.java:5243)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-16 02:59:12.900: E/AndroidRuntime(1443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-16 02:59:12.900: E/AndroidRuntime(1443):     ... 11 more

And this error appreas in this line:

points = (Point[]) myBundle.get("puntos");

I use this code to declare my Point array:

Point points[];

And I use this other code to put it on my bundle:

myIntent.putExtra("puntos", points);

I put my Point class code for more explication:

package com.example.en4dis;

public class Point implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = -4910520794768384111L;


String _id; 
String _comment; 
String _calification; 
String _coords;
int _X;
int _Y;

public Point(String id, String comment, String calification, String coords, int x, int y)
{
    _id = id; 
    _comment = comment; 
    _calification = calification; 
    _coords = coords;
    _X = x;
    _Y = y;     
}

public String getID()
{
    return _id;
}

public String getComment()
{
    return _comment;
}

public String getCalification()
{
    return _calification;
}

public String getCoords()
{
    return _coords;
}

public int getPointX()
{
    return _X;
}

public int getPointY()
{
    return _Y;
}

}

Eclipse throwed an exception and I have to put the "private static final long serialVersionUID = -4910520794768384111L;" line to fix it, I don't know if this line is the problem or what. Can someone help me? Thanks!

Was it helpful?

Solution

Just try,

replace

points = (Point[]) myBundle.get("puntos");

with

Object[] objects = myBundle.get("puntos");
points = Arrays.copyOf(objects ,objects.length, Point[].class);

Let me know what happen.

OTHER TIPS

You need the putSerializable method of the Bundle. Check this http://developer.android.com/reference/android/os/Bundle.html#putSerializable(java.lang.String, java.io.Serializable)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top