![](/img/trans.png)
[英]“System.NullReferenceException: Object reference not set to an instance of an object” when trying to insert ads using google AdMob Xamarin Android
[英]Xamarin android System.NullReferenceException: Object reference not set to an instance of an object
我是移動開發的新手。 我正在visual studio 2015
使用xamarin
開發 android。 我正在關注此示例代碼。 我正在我的設備上部署我的應用程序並且它運行良好。 我基本上有以下功能
所有功能都運行良好,但在更新時,當我首先單擊update layout
的更新按鈕時,它會從DB
(SQLITE)中獲取所有數據,然后將其顯示在“ Edit Text Boxes
在示例代碼中,您可以看到updateEmployee.cs
代碼. 所以在更新時我收到了波紋管錯誤
我在dName
獲得空值,而name
字段中有一個名稱
上面我已經聲明了dName
dEmail
等,如下所示
TextView dName, dEmail, dPhone, dDesignation;
String name, email, phone, designation;
同樣在initialize
方法中,我也得到空值,初始化中的eName
等是complete_data
布局中的id
,其 axml 如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:divider="#000"
android:dividerHeight="1dp" />
<TextView
android:id="@+id/eName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/eEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<TextView
android:id="@+id/ePhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<TextView
android:id="@+id/eDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation" />
</LinearLayout></ScrollView></LinearLayout>
更新 1:
波紋管是我收到此異常的代碼
void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
click_Employee = e.Position + 1;
ICursor c = dbHelper.getSingleEntry(click_Employee);
c.MoveToFirst();
name = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_NAME));
email = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_EMAIL));
phone = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_PHONE));
designation = c.GetString(c.GetColumnIndex(dbHelper.EMPLOYEE_DESIGNATION));
dName.Text = name;
dEmail.Text = email;
dPhone.Text = phone;
dDesignation.Text = designation;
}
name 字段在string
,我將此字符串分配給我的編輯文本dName
等,如上面的代碼所示。 在dName.text = name
我收到此null
異常,而 name 字段包含名稱。 有沒有其他方法可以做到?
我不確定我錯過了什么。 任何幫助將不勝感激
此問題最可能的原因是FindViewById
沒有被調用,或者它實際上沒有分配dname
。
這可能有多種原因:
initialize
方法永遠不會被調用List_ItemClick
之后調用TextView
並且在代碼中類型是EditText
設置兩個斷點。 一個在List_ItemClick
,一個在initialize
。 檢查首先調用哪個。 逐步執行initialize
以確保找到並分配視圖。
首先,我可以從您的代碼中看到您將 dName 聲明為 TextView,但是當您使用 FindViewById 時,您正在解析 EditText,因此請確保在兩個地方都使用 TextView。
其次,確保您的代碼按以下順序運行: 1- 將 dName 聲明為 TextView。 1- 為“name”變量賦值。 2- 使用 FindViewById 查找 eName。 3- 為 dName 分配一個值。
因此,您的代碼應如下所示:
TextView dName;
string name = "Some name"; // or you can get the name from database
dName = (TextView)FindViewById(Resource.Id.eName);
dName.Text = name;
這是一個常見的問題再次構建時,這里是我解決的步驟:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.