我有一个课,我在其中声明:
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;
}
}
然后是另一个类:
public class Comuni {
public static final Comune[] comuni = {
new Comune("A093|AGUGLIARO|1","AGUGLIARO"),
new Comune("A154|ALBETTONE|1","ALBETTONE"),
---
---
};
}
然后我有一个带有2个微调器和一个按钮的片段。当我点击微调器(是一个autocompletetextview,其中出现微调器)时,它会显示" Comune"我可以选择我想要的项目。当我有两个autocompletetextview的两个值并单击按钮时,我在第二个屏幕中传递了一个putExtra传递的2个值的旋转器,如下所示:
Intent intent = new Intent(getActivity(), SceltaFermateActivity.class);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_PARTENZA, itemPartenze);
intent.putExtra(SceltaFermateActivity.EXTRA_COMUNE_ARRIVO, itemArrivi);
startActivity(intent);
我尝试实现一个sqlite,我可以在列表视图中保存微调器的2个值,这样我就可以显示"最后的结果"。要从列表视图中检索值,我使用光标如:
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();*/
}
});
它有效!问题是,我会点击listview项目直接传递这两个值,依此类推它将打开第二个屏幕。但如果我通过
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);
崩溃是因为它说:String cannot be cast
解决方案?也许是一种在我点击项目时将字符串放入AutoCompleteTextView的方法吗?
日志:
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{com.dd.ftv/com.dd.ftv.SceltaFermateActivity}: 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