【问题标题】:Null reference error while inflating a textview膨胀文本视图时出现空引用错误
【发布时间】:2016-01-05 22:30:12
【问题描述】:

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

运行应用程序在 Activity 尝试使 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.

这是我的代码:

在 Activity .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" />

【问题讨论】:

    标签: c# android .net xamarin


    【解决方案1】:

    一些事情:

    您正在尝试扩充 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 引用,因此当LayoutInflator 尝试构建布局时,它会抛出 InflateException

    From the docs:

    android:drawableTop

    要在文本上方绘制的drawable。

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

    可以是颜色值,格式为“#rgb”、“#argb”、“#rrggbb”或“#aarrggbb”。

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

    最后,你不能在不使用适配器的情况下将子视图或标题视图添加到 ListView:

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

    【讨论】:

    • 谢谢。我需要一点时间来吸收这些信息,所以我稍后会回来。我确实发现 Xamarin 文档很难,他们似乎总是假设我没有知识。
    • S List 坚持下去,它确实需要一点时间来学习它。难度曲线很艰难,因为它涵盖了多种框架、语言和操作系统;我们都去过那里!继续努力吧:)
    • 我在RelativeLayout中封装了标题的TextView,并为它创建了一个id。然后我将此 id 传递给 Inflater。此外,我将文本和图像分成单独的标题。我已经有一个适配器了。
    • 完成这些更改后,我得到了一个“invalidOperation”异常,Clint 对此问题的回答解决了这个问题:stackoverflow.com/questions/4306324/…
    • 膨胀和添加标题现在可以工作了。非常感谢您的出色回答以及您的鼓励。
    猜你喜欢
    • 2016-09-29
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多