[英]Xamarin android unable to assign text value to edit text
我是移動開發的新手。 我正在使用Visual Studio 2015在xamarin中使用android進行工作。現在,我正在遵循示例代碼並按照其中的描述進行操作。
當我嘗試為update
布局中的edit text
分配text
值時,出現異常。 貝婁是我在其中分配值以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;
處dName.Text = name;
了異常dName.Text = name;
波紋管是錯誤的描述
System.NullReferenceException: Object reference not set to an instance of an object.
我的show complete data
功能執行了相同的工作,但是一切都很好,不知道update
功能正在發生什么
更新1
貝婁是我insert employee
代碼
namespace AppwithDB.Resources
{
[Activity(Label = "insertEmployee")]
class insertEmployee : Activity
{
EditText name, email, phone, designation;
Button submitBtn;
private SQLiteHelper dbHelper;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Create your app here
SetContentView(Resource.Layout.insert);
initialize();
dbHelper = new SQLiteHelper(this);
submitBtn.Click += submitBtn_Click;
}
private void submitBtn_Click(object sender, EventArgs e)
{
if (name.Text.ToString().Equals("") || email.Text.ToString().Equals("")|| phone.Text.ToString().Equals("") || designation.Text.ToString().Equals(""))
{
Toast.MakeText(this, "Fields Empty Found", ToastLength.Short).Show();
}
else
{
dbHelper.insertEmployee(name.Text.ToString(), email.Text.ToString(), phone.Text.ToString(), designation.Text.ToString());
Toast.MakeText(this, "Data Stored Successfully", ToastLength.Short).Show();
name.Text = " ";
email.Text = " ";
phone.Text = " ";
designation.Text = " ";
}
}
private void initialize()
{
name = (EditText)FindViewById(Resource.Id.empName);
email = (EditText)FindViewById(Resource.Id.empEmail);
phone = (EditText)FindViewById(Resource.Id.empPhone);
designation = (EditText)FindViewById(Resource.Id.empDesignation);
submitBtn = (Button)FindViewById(Resource.Id.insertEmpBtn);
}
}}
更新2
貝婁是update employee
的代碼
namespace AppwithDB.Resources
{
[Activity(Label = "updateEmployee")]
class updateEmployee:Activity
{
ListView list;
EditText dName, dEmail, dPhone, dDesignation;
String name, email, phone, designation;
SQLiteHelper dbHelper;
int click_Employee;
Button upBtn;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create your application here
SetContentView(Resource.Layout.update);
initialize();
dbHelper = new SQLiteHelper(this);
System.Collections.ArrayList dataList = dbHelper.getALLEmployeesData();
string[] myArr = (string[])dataList.ToArray(typeof(string));
list.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleExpandableListItem1, myArr);
list.ItemClick += List_ItemClick;
upBtn.Click += delegate
{
dbHelper.updateEmployeeInfo(click_Employee, dName.Text.ToString(), dEmail.Text.ToString(), dPhone.Text.ToString(), dDesignation.Text.ToString());
};
}
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);
}
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;
}
}}
更新3:
波紋管是complete_data
和update
的xmls
<?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>
更新xml
<?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="250dp"
android:divider="#000"
android:dividerHeight="1dp" />
<EditText
android:id="@+id/upName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/upEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="@+id/upPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Phone" />
<EditText
android:id="@+id/upDesignation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Designation" />
<Button
android:id="@+id/updateEmploy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update Employee" />
</LinearLayout></ScrollView></LinearLayout>
為了更好地理解,上面給出了示例代碼。
任何幫助將不勝感激
問題是您使用了錯誤的View
ID。 在您的XML中有upName
,並且您正在搜索Resource.Id.eName
。
要解決問題,您需要將Resource.Id.eName
更改為Resource.Id.upName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.