簡體   English   中英

Xamarin android System.NullReferenceException:未將對象引用設置為對象的實例

[英]Xamarin android System.NullReferenceException: Object reference not set to an instance of an object

我是移動開發的新手。 我正在visual studio 2015使用xamarin開發 android。 我正在關注此示例代碼 我正在我的設備上部署我的應用程序並且它運行良好。 我基本上有以下功能

  1. 插入
  2. 顯示
  3. 更新
  4. 刪除

所有功能都運行良好,但在更新時,當我首先單擊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

這可能有多種原因:

  1. 你的initialize方法永遠不會被調用
  2. List_ItemClick之后調用
  3. 它被調用但分配視圖失敗,因為在 XML 中使用有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;

這是一個常見的問題再次構建時,這里是我解決的步驟:

  1. 如果您啟用了 xamarin 自動更新,則檢查您的項目設置和目標 Android 版本,如果已更新,則 (a) 將其降級或 (b) 通過轉到工具--> SDK 管理器下載新版本的 SDK
  2. 使用所有 APP 實例清理您的模擬器
  3. 清潔構建
  4. 關閉計算機..然后啟動系統(不要重新啟動它)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM