使用SimpleAdapter和Mono for Android

时间:2012-07-05 15:27:25

标签: android mono xamarin.android

我在Mono for Android中创建了一个FileDialog活动。但是,在我看来,使用ListView simpleadapter时,某些MonoObject到JavaObject的转换存在问题。 在我离开onCreate之后,VM崩溃了以下堆栈跟踪:

07-05 15:04:46.493 D/FileDialogActivity( 3430): Left OnCreate
07-05 15:04:46.503 D/AndroidRuntime( 3430): Shutting down VM
07-05 15:04:46.503 W/dalvikvm( 3430): threadid=1: thread exiting with uncaught exception (group=0xb4127180)
07-05 15:04:46.523 D/dalvikvm( 3430): GC_CONCURRENT freed 115K, 3% free 9634K/9863K, paused 0ms+2ms
07-05 15:04:46.523 E/AndroidRuntime( 3430): FATAL EXCEPTION: main
07-05 15:04:46.523 E/AndroidRuntime( 3430): java.lang.ClassCastException: mono.android.runtime.JavaObject cannot be cast to java.util.Map
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:147)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.ListView.makeAndAddView(ListView.java:1772)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.ListView.fillDown(ListView.java:672)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.ListView.fillFromTop(ListView.java:732)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.ListView.layoutChildren(ListView.java:1625)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.AbsListView.onLayout(AbsListView.java:1863)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.View.layout(View.java:11278)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.os.Looper.loop(Looper.java:137)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-05 15:04:46.523 E/AndroidRuntime( 3430):     at dalvik.system.NativeStart.main(Native Method)

一些代码:

IList<IDictionary<string, object>> mData = new List<IDictionary<string, object>>();
AddFiles(files, mData); // Which just new Dictionary with filedata and icon from a list of files.
SimpleAdapter fileAdapter = new SimpleAdapter(this, mData,
            Resource.Layout.FileDialogRow,
            new string[] { FileKey, FileImage },
            new int[] { Resource.Id.fdrowname, Resource.Id.fdrowimage }
        );
ListAdapter = fileAdapter;
Log.Debug("Left OnCreate");

在Mono for Android上使用SimpleAdapter时,其他人都有问题吗?或者对这里发生的事情有任何线索?

1 个答案:

答案 0 :(得分:4)

我认为您需要使用List而不是JavaList。像:

var mData = new JavaList<IDictionary<string, object>>();

这个帖子有一个很长的解释:http://mono-for-android.1047100.n5.nabble.com/SimpleAdapter-break-in-4-2-1-td5709793.html

我将在列表中使用更复杂的东西我建议实现自己的适配器,最后维护和调试要容易得多。