2016-01-05 37 views
3

我在Xamarin Android应用程序中将textview添加为列表视图。 按照ericosg对this SO question的回答中的说明,我将textview放在一个单独的axml文件中,然后尝试将它作为标题添加到我的列表视图中。在为文本视图添加空引用时出错null

运行的应用程序在那里的活动试图夸大TextView的行得到以下错误:

[MonoDroid] UNHANDLED EXCEPTION: 
[MonoDroid] Android.Content.Res.Resources+NotFoundException: Exception of type 'Android.Content.Res.Resources+NotFoundException' was thrown. 
. 
. 
. 
[MonoDroid] --- End of managed exception stack trace --- 
[MonoDroid] android.content.res.Resources$NotFoundException: Resource ID #0x7f050006 type #0x12 is not valid 
[MonoDroid]  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2250) 
etc. 

这里是我的代码:

在活动.cs文件:

myListView = FindViewById<ListView>(MyApp.Resource.Id.MyList); 

     Android.Views.View myHeader = 
      this.LayoutInflater.Inflate(MyApp.Resource.Id.QuestionText, myListView); 

     myListView.AddHeaderView(myHeader); 

ListView的定义:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView 
     android:id="@+id/MyList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    </RelativeLayout> 

头定义:

<?xml version="1.0" encoding="utf-8"?> 
     <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/QuestionText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:drawableTop="@+id/MyImage" /> 

回答

3

有几件事情:

您正试图夸大的,而不是一个布局的ID资源:

Android.Views.View myHeader = this.LayoutInflater.Inflate(MyApp.Resource.Id.QuestionText, myListView); 

Inflate呼吁LayoutInflater只接受Resource.Layout元素。将其更改为:

Android.Views.View myHeader = this.LayoutInflater.Inflate(Resource.Layout.Header, myListView); 

其次,在你的Header.xml布局文件,TextView android:drawableTop不接受ID引用,这样它会抛出一个InflateExceptionLayoutInflator尝试建立布局。

From the docs

安卓drawableTop

被拉伸到上面的文字可以得出。

可能是以“@ [+] [package:] type:name”形式对另一个资源的引用,或以“?[package:] [type:] name”的形式指向主题属性。

可能是一个颜色值,形式为“#rgb”,“#argb”,“#rrggbb”或“#aarrggbb”。

将此更改为对有效颜色或可绘制(@ drawable/MyImage)或内联颜色声明(#fff)的引用。

最后,你不能没有使用适配器添加子视图或头视图到ListView:

考虑重新阅读Xamarin docs for ListViews and Adapters更好地理解主题。

+3

谢谢。我需要一点时间才能吸收这些信息,所以我稍后会回来。我发现Xamarin的文档很难,他们似乎总是假设我没有的知识。 –

+1

坚持S列表,它*会花费一点时间来学习它。困难的曲线是艰难的,因为它涵盖了多种框架,语言和操作系统;我们都在那里!只是继续削减它:) – matthewrdev

+0

我在一个RelativeLayout中包含了标题的TextView,并为它创建了一个id。然后我把这个ID传给了Inflater。此外,我将文本和图像分隔为不同的标题。我已经有一个适配器。 –