新手在这里。
我的第一个使用Xamarin的应用程序有例外:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
主要活动:
public class MainActivity : Activity
{
List<TypeOfClasses> listOfClasses;
ListView listView;
ListArrayAdapter adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.myButton);
listOfClasses = new List<TypeOfClasses>();
listView = FindViewById<ListView>(Resource.Id.listView);
adapter = new ListArrayAdapter(this, Resource.Layout.List_item, listOfClasses);
listView.Adapter = adapter;
button.Click += delegate
{
var intent = new Intent(this, typeof(SecondActivity));
StartActivityForResult(intent, 0);
};
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
TypeOfClasses toc = new TypeOfClasses();
if (resultCode= Result.Ok)
{
toc.Subj = data.GetStringExtra("SUBJECT");
toc.Lect = data.GetStringExtra("LECTURER");
toc.Date = data.GetStringExtra("DATE");
listOfClasses.Add(toc);
adapter.NotifyDataSetChanged();
}
}
}
SecondActivity类,用于在MainActivity中传输数据:
public class SecondActivity : Activity
{
public string value;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Second);
EditText edtTxtSubj = FindViewById<EditText>(Resource.Id.editTextSubject);
EditText edtTxtDt = FindViewById<EditText>(Resource.Id.editTextDate);
EditText edtTxtLec = FindViewById<EditText>(Resource.Id.editTextLecturer);
Button buttonAdd = FindViewById<Button>(Resource.Id.buttonAdd);
buttonAdd.Click += delegate
{
var intentSecond = new Intent(this, typeof(SecondActivity));
intentSecond.PutExtra("SUBJECT", edtTxtSubj.Text.ToString());
intentSecond.PutExtra("LECTURER", edtTxtLec.Text.ToString());
intentSecond.PutExtra("DATE", edtTxtDt.Text.ToString());
SetResult(Result.Ok, intentSecond);
Finish();
};
}
}
ListArrayAdapter:
public class ListArrayAdapter : ArrayAdapter<TypeOfClasses>
{
List<TypeOfClasses> list;
Activity context;
int resource;
public ListArrayAdapter(Activity context, int resource, List<TypeOfClasses> items)
: base(context, resource, items)
{
this.context = context;
this.list = items;
this.resource = resource;
}
public override int Count
{
get { return list.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(LabProject1.Resource.Layout.List_item, null);
var txtSubj = view.FindViewById<TextView>(LabProject1.Resource.Id.textViewSubject);
var txtLect = view.FindViewById<TextView>(LabProject1.Resource.Id.textViewLecturer);
var txtDate = view.FindViewById<TextView>(LabProject1.Resource.Id.textViewDate);
txtSubj.Text = list[position].Subj;
txtLect.Text = list[position].Lect;
txtDate.Text = list[position].Date;
return convertView;
}
}
类TypeOfClasses包含诸如Subj,Lect,Date之类的属性。
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ADD ELEMENTS" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView3" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView2" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</LinearLayout>
Second.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="SUBJECT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextSubject" />
<TextView
android:text="LECTURER"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextLecturer" />
<TextView
android:text="DATE"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextDate" />
<TextView
android:text="TYPE OF SUBJECT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4" />
<Button
android:text="ADD A NEW EVENT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonAdd" />
</LinearLayout>
List_item.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewSubject" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewLecturer" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewDate" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewType" />
</LinearLayout>
最佳答案
在ListArrayAdapter
类中,您将返回convertView
。如果没有要转换的视图,则此变量为null
。您需要返回您的view
变量:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
//... your existing code
//return your view
return view;
}
我想这只是一个小小的疏忽,该方法内部的总体逻辑是正确的(
convertView
在不是null
时可以重用,如Android中所建议)。