dimanche 19 avril 2015

String cannot be cast to Object

I have a class in which i declare this:



public class Comune implements Serializable {
public String id;
public String name;

public Comune(String id, String name) {
this.id = id;
this.name = name;
}

@Override
public String toString() {
return name;
}

}


then another class like:



public class Comuni {

public static final Comune[] comuni = {
new Comune("A093|AGUGLIARO|1","AGUGLIARO"),
new Comune("A154|ALBETTONE|1","ALBETTONE"),
---
---
};
}


then i have a fragment with 2 spinners and a button. When i click in the spinner (is an autocompletetextview where appears the spinner in there) it shows the list of "Comune" and i can choose the item i want. When i have the two values of the two autocompletetextview and click in the button i pass in the second screen passing with a putExtra the 2 values of the spinners like this:



Intent intent = new Intent(getActivity(), SceltaFermateActivity.class);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_PARTENZA, itemPartenze);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_ARRIVO, itemArrivi);
startActivity(intent);


I tried to implement an sqlite in which i am able to save the 2 values of the spinner in a listview so i can show the "last result". To retrieve the values from the listview i use the cursor like:



lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String item_partenza = cursor.getString(cursor.getColumnIndex(mdb.KEY_PARTENZA));
String item_arrivo = cursor.getString(cursor.getColumnIndex(mdb.KEY_ARRIVO));


/*String item = String.valueOf(item_id) + " : " + item_partenza +"\n"+item_arrivo;
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();*/
}
});


and it works! The problem is that i would on click on the listview item pass the two values directly so on Click it will open the second screen. But if i pass



Intent intent = new Intent(getActivity(), SceltaFermateActivity.class);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_PARTENZA, item_partenza);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_ARRIVO, item_arrivo);
startActivity(intent);


it crashes because it says: String cannot be cast solutions? Maybe a way to put the strings in the AutoCompleteTextView when i click on the item?


log:



04-20 00:54:48.284: E/AndroidRuntime(29566): FATAL EXCEPTION: main
04-20 00:54:48.284: E/AndroidRuntime(29566): Process: com.dd.ftv, PID: 29566
04-20 00:54:48.284: E/AndroidRuntime(29566): java.lang.RuntimeException: Unable to start activity ComponentInfo{http://ift.tt/1D3uh1R}: java.lang.ClassCastException: java.lang.String cannot be cast to it.ftv.app.model.Comune
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.os.Looper.loop(Looper.java:135)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.ActivityThread.main(ActivityThread.java:5223)
04-20 00:54:48.284: E/AndroidRuntime(29566): at java.lang.reflect.Method.invoke(Native Method)
04-20 00:54:48.284: E/AndroidRuntime(29566): at java.lang.reflect.Method.invoke(Method.java:372)
04-20 00:54:48.284: E/AndroidRuntime(29566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
04-20 00:54:48.284: E/AndroidRuntime(29566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
04-20 00:54:48.284: E/AndroidRuntime(29566): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
04-20 00:54:48.284: E/AndroidRuntime(29566): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to it.ftv.app.model.Comune
04-20 00:54:48.284: E/AndroidRuntime(29566): at com.dd.ftv.SceltaFermateActivity.onCreate(SceltaFermateActivity.java:72)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.Activity.performCreate(Activity.java:5933)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-20 00:54:48.284: E/AndroidRuntime(29566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-20 00:54:48.284: E/AndroidRuntime(29566): ... 11 more

Aucun commentaire:

Enregistrer un commentaire