Null reference error expanding textview

I added textview as a title to listview in xamarin android app. According to the instructions in ericosg's answer to this so question, I put textview in a separate AXML file, and then try to add it as a header to my list view

Running the application will receive the following error on the line where the activity attempts to inflate the 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.

This is my code:

In the activity. CS file:

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

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

        myListView.AddHeaderView(myHeader);

Listview definition:

<?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>

Header definition:

<?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" />

resolvent:

Some things:

You are trying to inflate the ID resource instead of the layout:

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

The insert call of layouteinflator only accepts the resource.layout element. Change to:

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

Secondly, in your header.xml layout file, textview Android: drawabletop does not accept ID references, so when layoutinterpreter tries to build a layout, it will throw an inflateexception

From the docs:

Change it to a reference to a valid color or paintable object (@ drawable / myimage) or an inline color declaration (#fff)

Finally, you cannot add a subview or title view to the listview without an adapter:

> https://stackoverflow.com/a/7978427/1099111 > https://stackoverflow.com/a/7978427/1099111

Consider rereading xamarin docs for listviews and adapters to better understand the topic

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>