我是移动开发的新手。我正在使用visual studio 2015在xamarin中使用android。现在我正在关注sample code并按照其中所述进行操作。
我尝试在text
布局中为edit text
分配update
值时收到异常。 Bellow是我将值分配给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;
引发了异常.Bellow是错误描述
System.NullReferenceException: Object reference not set to an instance of an object.
我的show complete data
函数执行了相同的工作,但事情很好,不知道update
函数中发生了什么
更新1
Bellow是我的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
Bellow是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:
Bellow是xmls
和complete_data
update
<?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>
为了更好地理解,上面给出了示例代码。
任何帮助都将受到高度赞赏
答案 0 :(得分:0)
问题是您使用了错误的View
ID。您的XML中有upName
,您正在搜索Resource.Id.eName
要解决问题,您需要将Resource.Id.eName
更改为Resource.Id.upName