Xamarin如何分配字符串来编辑文本?

时间:2016-11-08 09:33:22

标签: c# android xamarin.android

我是移动开发的新手。我正在xamarin使用visual studio 2015处理android。当我将字符串分配给edit text时,我得到一个空引用异常。贝娄是代码

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;
    }

我得到的例外是dName.text = name点。为了更好地理解,请参阅下面的代码

ListView list; 
    EditText dName, dEmail, dPhone, dDesignation;
    String name, email, phone, designation;
    SQLiteHelper dbHelper;
    int click_Employee;
    Button upBtn;

 private void initialize()
    {
        list = (ListView)FindViewById(Resource.Id.listView1);
        dName = (EditText)FindViewById(Resource.Id.eName);
        dEmail = (EditText)FindViewById(Resource.Id.eEmail);
        dPhone = (EditText)FindViewById(Resource.Id.ePhone);
        dDesignation = (EditText)FindViewById(Resource.Id.eDesignation);
        upBtn = (Button)FindViewById(Resource.Id.updateEmploy);
    }

Bellow是axml,当选择记录时数据进入update layout

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

如需更多理解,请参阅此link我正在使用并将其作为示例代码

我也在this链接中完成了推荐,但结果是相同的

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您已在布局中将项目设置为TextViews,但是您将它们转换为类中的EditTexts。

<EditText
        android:id="@+id/eName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone" />

您还应该键入而不是如下所示进行转换。

dName = FindViewById<EditText>(Resource.Id.eName);