0
にキャストすることはできません私はDrawerLayout
はandroid.support.v4.app.Fragment(XamarinのC#)
をしようしかし、私はここで問題
Java.Lang.ClassCastException: tieit.droid.MyListFragment cannot be cast to android.support.v4.app.Fragment
が直面しているMainViewModelコードで私のコードです:
using MvvmCross.Core.ViewModels;
using System;
using System.Collections.Generic;
namespace TieiT.Core
{
public class MainViewModel : MvxViewModel
{
readonly Type[] _menuItemTypes = {
typeof(MyListViewModel),
typeof(MySettingsViewModel),
};
public IEnumerable<string> MenuItems { get; private set; } = new [] { "My List", "My Settings" };
public void ShowDefaultMenuItem()
{
NavigateTo (0);
}
public void NavigateTo (int position)
{
ShowViewModel (_menuItemTypes [position]);
}
}
public class MenuItem : Tuple<string, Type>
{
public MenuItem (string displayName, Type viewModelType)
: base (displayName, viewModelType)
{}
public string DisplayName
{
get { return Item1; }
}
public Type ViewModelType
{
get { return Item2; }
}
}
}
私はエラーがある場合、それはこれをhightlights:
public void NavigateTo (int position)
{
ShowViewModel (_menuItemTypes [position]);
}
01ここ
はMyListFragmentです:
using Android.OS;
using Android.Runtime;
using Android.Views;
using MvvmCross.Droid.Shared.Attributes;
using MvvmCross.Droid.FullFragging.Fragments;
using TieiT.Core;
namespace TieiT.Droid
{
[MvxFragmentAttribute(typeof(MainViewModel), Resource.Id.frameLayout)]
[Register("tieit.droid.MyListFragment")]
public class MyListFragment : MvxFragment<MyListViewModel>
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.MyListView, container, false);
}
}
}
そして、ここでは、私がDrawerLayout
を表示する必要が私の活動です:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using MvvmCross.Droid.Support.V7.AppCompat;
using TieiT.Droid;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Toolbar = Android.Support.V7.Widget.Toolbar;
using Fragment = Android.Support.V4.App.Fragment;
using TieiT.Core;
namespace TieiT.Droid.Views
{
[Activity]
public class GeneralInfo : MvxCachingFragmentCompatActivity<MainViewModel>
{
ActionBarDrawerToggle _drawerToggle;
ListView _drawerListView;
DrawerLayout _drawerLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.GeneralInfoPage);
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
_drawerListView = FindViewById<ListView>(Resource.Id.drawerListView);
_drawerListView.ItemClick += (s, e) => ShowFragmentAt(e.Position);
_drawerListView.Adapter = new ArrayAdapter<string>(this, global::Android.Resource.Layout.SimpleListItem1, ViewModel.MenuItems.ToArray());
_drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);
_drawerToggle = new ActionBarDrawerToggle(this, _drawerLayout, Resource.String.OpenDrawerString, Resource.String.CloseDrawerString);
_drawerLayout.SetDrawerListener(_drawerToggle);
ShowFragmentAt(0);
}
void ShowFragmentAt(int position)
{
ViewModel.NavigateTo(position);
Title = ViewModel.MenuItems.ElementAt(position);
_drawerLayout.CloseDrawer(_drawerListView);
}
protected override void OnPostCreate(Bundle savedInstanceState)
{
_drawerToggle.SyncState();
base.OnPostCreate(savedInstanceState);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (_drawerToggle.OnOptionsItemSelected(item))
return true;
return base.OnOptionsItemSelected(item);
}
}
}
私はこのエラーを解決することができますか?
ありがとうございました。
UPDATE
私の主なレイアウト:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- content must go before menu because of z-order -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- menu -->
<ListView
android:id="@+id/drawerListView"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground" />
</android.support.v4.widget.DrawerLayout>
そして、ここでは私がコンソールに持っているものです。
[ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.ClassCastException: tieit.droid.MyListFragment cannot be cast to android.support.v4.app.Fragment
10-01 19:52:15.838 E/mono-rt (3039): --- End of managed Java.Lang.ClassCastException stack trace ---
10-01 19:52:15.838 E/mono-rt (3039): java.lang.ClassCastException: tieit.droid.MyListFragment cannot be cast to android.support.v4.app.Fragment
レイアウトにxmlとstacktraceを追加してください。 –
投稿更新@StefanWanitzek – Eugene