设置文本或图像给出NullReferenceException
本文关键字:NullReferenceException 图像 置文本 | 更新日期: 2023-09-27 18:03:27
如标题所述,当给ImageView
或TextView
赋值时,我得到一个NullReferenceException
。
下面是我得到的错误:
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在列表的底部(因此尚未启用)引起的,并且在这样做时,它的一切都是null,直到它是。所以我把它移到了function的顶部
问题很简单-在您的xml中没有您在代码中寻找的用户控件。例如第51行:"itemPicture",但是在http://pastebin.com/jjJUUzHw中没有这样的控件我看到你有两个布局- Resource.Layout.Main和Resource.Layout.Drinks,但你只提交了一个xml,所以可能我找错人了
这样工作后,我发现错误是由setContentView在列表的底部(因此尚未启用)引起的,并且在这样做时,它的一切都是空的,直到它是。所以我把它移到了function的顶部