Xamarin how to assign a string to edit text ?

被刻印的时光 ゝ 提交于 2020-01-06 17:09:48

问题


I am new to mobile development. I am working on android in xamarin using visual studio 2015. I am getting a null reference exception when i am assigning a string to an edit text. Bellow is the code

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

The exception i am getting is at point dName.text = name. For better understanding please see the bellow code

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 is my axml from where the data is coming in update layout when a record is selected

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

For more understanding please see this link i am using and following it as a sample code

Also i have done the recommends in this link but result is the same

Any help would be appreciated


回答1:


You've set the items as TextViews in the layout but you're casting them to EditTexts in your class.

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

You should also do typing rather than casting as shown below.

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


来源:https://stackoverflow.com/questions/40483353/xamarin-how-to-assign-a-string-to-edit-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!