Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 添加选择器时,在MvxListview中用作项的TextView上引发TargetInvocationException_Android_Android Listview_Xamarin.android_Mvvmcross - Fatal编程技术网

Android 添加选择器时,在MvxListview中用作项的TextView上引发TargetInvocationException

Android 添加选择器时,在MvxListview中用作项的TextView上引发TargetInvocationException,android,android-listview,xamarin.android,mvvmcross,Android,Android Listview,Xamarin.android,Mvvmcross,我使用的是一个MvxListView和一个FrameLayout,该FrameLayout包含一个片段,详细描述了所选项目的信息。在我需要从文件加载一个项目并以编程方式选择它之前,它工作得很好:我使用SetItemChecked(position,true)设置的所选项目将不再突出显示 因此,我按照回答中的说明进行操作,该说明指示我使用选择器来根据状态更改显示。问题是,当我将选择器添加到我的文本视图中时,如中所示: android:textColor="@drawable/list_item_t

我使用的是一个
MvxListView
和一个FrameLayout,该FrameLayout包含一个片段,详细描述了所选项目的信息。在我需要从文件加载一个项目并以编程方式选择它之前,它工作得很好:我使用
SetItemChecked(position,true)
设置的所选项目将不再突出显示

因此,我按照回答中的说明进行操作,该说明指示我使用
选择器来根据状态更改显示。问题是,当我将
选择器添加到我的
文本视图中时,如中所示:

android:textColor="@drawable/list_item_text_selector"
如果我试图显示我的活动,我将得到一个
TargetInvocationException
,请参见下面堆栈的第一行:

MvxBind:Error: 10.52 Exception during creation of TextView from type Android.Widget.TextView - exception TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <filename unknown>:0 
at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0 
at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0 
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <filename unknown>:0 
at Cirrious.MvvmCross.Binding.Droid.Binders.MvxAndroidViewFactory.CreateView (System.String name, Android.Content.Context context, IAttributeSet attrs) [0x00000] in <filename unknown>:0 
布局文件夹中的mysitelistitem.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        local:MvxBind="Text Name"
        android:padding="16dp"
        android:textSize="20sp"
        android:textColor="@drawable/list_item_text_selector" />
</LinearLayout>

从可绘图文件夹中列出项目文本选择器.xml

<?xml version="1.0" encoding="utf-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:state_activated="true"
      android:color="@color/item_selected_text"/>
  <item
      android:state_pressed="true"
      android:color="@color/item_selected_text"/>
  <item
      android:state_pressed="false"
      android:color="@color/item_selected_text"/>
</Selector>


(事实上,我找到了一个可以证明是令人满意的解决方案的答案(请参阅),但它没有使用
选择器
,在我看来,它感觉更好,所以我很好奇如何使用
选择器
)来解决这个问题)

如果你的选择器不是在textColor上,而是在背景drawable上怎么办?我在看这个例子@PkL728,我实现了您提供的例子,没有其他例外,但我也看不到选择器的任何效果..:/好的,所以在周末我的大脑恢复正常后,我意识到我决定使用
选择器(项目不会突出显示)的最初原因是我的错误:我使用了一个错误的索引来设置所选项目。。。这么愚蠢的错误。。。我不知道为什么我之前没想到。。。不管怎么说,问题是关于使用
选择器的,而这个特定的问题没有解决,所以我会让它打开,直到提供满意的答案。但我将继续我的代码,所以我可能无法立即测试答案。
<?xml version="1.0" encoding="utf-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:state_activated="true"
      android:color="@color/item_selected_text"/>
  <item
      android:state_pressed="true"
      android:color="@color/item_selected_text"/>
  <item
      android:state_pressed="false"
      android:color="@color/item_selected_text"/>
</Selector>