设置Text或Image会产生NullReferenceException

时间:2015-07-20 10:44:34

标签: c# android xamarin xamarin.android

如标题中所述,我在为NullReferenceExceptionImageView分配值时会获得TextView

这是我得到的错误:

07-20 11:33:04.833 I/MonoDroid(11396): UNHANDLED EXCEPTION:
07-20 11:33:04.843 I/MonoDroid(11396): System.NullReferenceException: Object reference not set to an instance of an object
07-20 11:33:04.843 I/MonoDroid(11396): at JittersApp.Droid.LayoutCodes.Drinks.listCompare (string,JittersApp.Droid.MainActivity) [0x00043] in d:\Docs\Eamonns App Development\App\App Core\JittersApp\JittersApp\JittersApp.Droid\LayoutCodes\Drinks.cs:61
07-20 11:33:04.843 I/MonoDroid(11396): at JittersApp.Droid.LayoutCodes.Drinks/<>c__DisplayClass2.<coreDrinks>b__1 (object,Android.Widget.AdapterView/ItemClickEventArgs) [0x00018] in d:\Docs\Eamonns App Development\App\App Core\JittersApp\JittersApp\JittersApp.Droid\LayoutCodes\Drinks.cs:45
07-20 11:33:04.843 I/MonoDroid(11396): at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000d] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:283
07-20 11:33:04.843 I/MonoDroid(11396): at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00019] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:211
07-20 11:33:04.843 I/MonoDroid(11396): at (wrapper dynamic-method) object.f7f74bf0-78c1-4c81-a8d9-11a0a0130a01 (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x0009b>
07-20 11:33:04.853 W/dalvikvm(11396): VFY: unable to resolve direct method 44: Ljava/lang/Throwable;.<init> (Ljava/lang/String;Ljava/lang/Throwable;ZZ)V
07-20 11:33:04.863 W/dalvikvm(11396): threadid=1: thread exiting with uncaught exception (group=0x41a64438)
An unhandled exception occured.

我的代码如下:

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 Android.Graphics;
using Android.Graphics.Drawables;

namespace JittersApp.Droid.LayoutCodes
{
    public class Drinks
    {
        void listSetUp()
        {

        }
        string[] items = new string[] { "Cat", "Mouse", "Dog" };

        public void coreDrinks(MainActivity mainAc)
        {

            //Home Click

            ImageView iv = mainAc.FindViewById<ImageView>(Resource.Id.homeDrinks);
            iv.Click += delegate
            {
                Resuables r = new Resuables();
                r.onHomeClick(mainAc);
            };

            IListAdapter la = new ArrayAdapter(mainAc, Android.Resource.Layout.SimpleListItem1, items);

            ListView lv = mainAc.FindViewById<ListView>(Resource.Id.drinksListView);
            lv.SetAdapter(la);

            lv.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
            {
                String selectedFromList = lv.GetItemAtPosition(e.Position).ToString();
                listCompare(selectedFromList, mainAc);

            };

        }

        private void listCompare(string selectedFromList, MainActivity av)
        {
            Resuables r = new Resuables();
            if (selectedFromList == items[0])
            {
                ImageView iv = av.FindViewById<ImageView>(Resource.Id.itemPicture);
                //iv.SetImageResource(Resource.Drawable.JL);
                TextView tv = av.FindViewById<TextView>(Resource.Id.itemDes);
                //tv.SetText(Resource.String.firstItem);
                TextView price = av.FindViewById<TextView>(Resource.Id.textPrice);
                //price.SetText(Resource.String.three_pounds);
                av.SetContentView(Resource.Layout.ItemViewer);

            }
        }



    }
}

第57,59和61行(注释行)导致相同的错误。有什么想法会导致这种情况吗?

这让我感到烦恼,因为它与网站上的代码完全相同(事实上它是出自MainActivity类的,但是如果我按照我的方式设置那么这应该没关系?)哦和coreDrinks函数从MainActivity类本身调用。

编辑:

XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="#ffa95d5d">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@drawable/jb"
            android:layout_width="261.2dp"
            android:layout_height="103.4dp"
            android:id="@+id/imageView1"
            android:layout_marginRight="0.0dp"
            android:scaleType="fitXY" />
        <ImageView
            android:src="@drawable/basket_shopping_2"
            android:layout_width="48.5dp"
            android:layout_height="match_parent"
            android:id="@+id/imageView2" />
    </LinearLayout>
</LinearLayout>

MainActivity:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using JittersApp.Droid.LayoutCodes;


namespace JittersApp.Droid
{
    [Activity(Label = "JittersApp.Droid", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]
        public class MainActivity : Activity
        {

                protected override void OnCreate (Bundle bundle)
                {
                        base.OnCreate (bundle);

                        // Set our view from the "main" layout resource
                        SetContentView (Resource.Layout.Main);
            Core();
                }

        public void Core()
        {
            //Drinks Button
            Button dk = FindViewById<Button>(Resource.Id.drinksBtn);
            dk.Click += delegate
            {
                SetContentView(Resource.Layout.Drinks);
                Drinks dr = new Drinks();
                dr.coreDrinks(this);
            };

        }

        }
}
编辑:经过这样的工作后,我发现错误是由于setContentView位于列表的底部(因此尚未启用),并且在执行此操作时,其上的所有内容都为空,直到它为止。所以我把它移到了功能的顶端。

2 个答案:

答案 0 :(得分:0)

问题很简单 - 在你的xml中没有你在代码中寻找的用户控件。例如第51行:&#34; itemPicture&#34;,但没有这样的控件http://pastebin.com/jjJUUzHw 我看到你有两个布局 - Resource.Layout.Main和Resource.Layout.Drinks,但是你只提交了一个xml,所以我可能会在错误的树上咆哮。

答案 1 :(得分:0)

经过这样的工作,我发现错误是由于setContentView位于列表的底部(因此尚未启用),并且在执行此操作时,其上的所有内容都为空,直到它为止。所以我把它移到了功能的顶端。