我正在尝试将适配器用于我自己的列表,但添加了listview_row.axml,如下所示,布局,当我尝试运行我的应用程序时调试我从模拟器获取消息“不幸的是Android App已停止“和Visual Studio中的未处理异常:System.NullReferenceException: Object reference not set to an instance of an object.
和Android Resources$NotFoundException: Resource ID #0x7f030001
不确定这里有什么问题:
MyListWievAdapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Android_App1
{
class MyListViewAdapter : BaseAdapter<string>
{
private List<string> mItems;
private Context mContext;
public MyListViewAdapter(Context context, List<string> items)
{
mItems = items;
mContext = context;
}
public override int Count
{
get { return mItems.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override string this[int position]
{
get { return mItems[position]; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);
}
TextView textName = row.FindViewById<TextView>(Resource.Id.textName);
textName.Text = mItems[position];
return row;
}
}
}
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
namespace Android_App1
{
[Activity(Label = "Android_App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private List<string> mItems;
private ListView mlistView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mlistView = FindViewById<ListView>(Resource.Id.listView1);
mItems = new List<string>();
mItems.Add("Name1");
mItems.Add("Name2");
mItems.Add("Name3");
MyListViewAdapter adapter = new MyListViewAdapter(this, mItems);
string indexerTest = adapter[1];
mlistView.Adapter = adapter;
}
}
}
并在布局文件夹中:
listview_row.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"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textName" />
</LinearLayout>
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">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</LinearLayout>