public class CustomAdapter : BaseAdapter{
private const int TYPE_ITEM = 0;
private const int TYPE_SEPARATOR = 1;
private List<string> mData;
private TreeSet sectionHeader;
private LayoutInflater mInflater;
public CustomAdapter(Context context, List<string> Data) {
mInflater = (LayoutInflater) context
.GetSystemService(Context.LayoutInflaterService);
this.mData=Data;
}
public void addItem(string item) {
mData.Add(item);
NotifyDataSetChanged();
}
public void addSectionHeaderItem(string item) {
mData.Add(item);
//sectionHeader.Add(mData.Count - 1);
NotifyDataSetChanged();
}
public int getItemViewType(int position) {
return sectionHeader.Contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
public int getViewTypeCount {
get{ return 2; }
}
public override int Count {
get {return mData.Count;}
}
public override Java.Lang.Object GetItem(int position) {
return mData[position];
}
public override long GetItemId(int position) {
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.Inflate(Resource.Layout.textViewItemsSeparator, parent);
holder.textView = (TextView) convertView.FindViewById(Resource.Id.textviewHeaderItems);
break;
case TYPE_SEPARATOR:
convertView = mInflater.Inflate(Resource.Layout.textViewHeaderItems, parent);
holder.textView = (TextView) convertView.FindViewById(Resource.Id.textviewItemsSeparator);
break;
}
convertView.Tag=holder;
} else {
holder = (ViewHolder)convertView.Tag as ViewHolder;
}
holder.textView.Text=mData[position];
return convertView;
}
public class ViewHolder:Java.Lang.Object {
public TextView textView;
}
}
&#13;
ListView lst;
string[] items = new string[] { "Alternative Rock","Classical",...........};
List<string> listItems;
private CustomAdapter mAdapter;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Create your fragment here
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
listItems = new List<string> (items);
return inflater.Inflate (Resource.Layout.GenerFragment, container, false);
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
lst = View.FindViewById<ListView> (Resource.Id.lstGenres);
//lst.Adapter = new ArrayAdapter<string>(Activity, Resource.Layout.textViewHeaderItems,Resource.Id.textviewHeaderItems, items);
//lst = View.FindViewById<ListView> (Resource.Id.lst_genre);
//lst.SetAdapter(new ArrayAdapter<String>(this.Activity, Resource.Layout.GenerFragment, items));
//mAdapter=new CustomAdapter();
for (int i = 0; i < listItems.Count(); i++) {
mAdapter.addItem (listItems[i]);
if (i == 0) {
mAdapter.addSectionHeaderItem ("Music");
} else if(i==13) {
mAdapter.addSectionHeaderItem ("Audio");
}
}
lst.Adapter = new CustomAdapter (Activity, listItems);
&#13;
我花了很多时间寻找错误,但我不知道为什么它是null。虽然它从列表中获得了数据
mAdapter.addItem(listItems [i]); - &GT;我在设备上调试时出现null异常。哪里不对?
答案 0 :(得分:1)
OnActivityCreated
中的您正在引用listItems
for (int i = 0; i < listItems.Count(); i++) {
但是,listItems
为空。您在OnCreateView
初始化它,尚未执行。在尝试引用listItems
之前,您需要确保private CustomAdapter mAdapter;
已初始化。
此外,您正在尝试向mAdapter添加项目,但它从未被初始化(据我所见)
你在这里声明它,但在你将其初始化之前它将是NULL
//mAdapter=new CustomAdapter();
这是初始化,已注释掉
mAdapter.addItem (listItems[i]);
当你试图在这里引用它时,它仍然是null,并将抛出一个空引用异常
define(MAX_SLEEP, 3); // Decide a good value for number of tries
$sleep = 0; // Initialize value, always a good habit from C :)
$done = false; // Sentinel value
$flock = new Flock; // You need to implement this class
do {
if (! $flock->locked()) { // We have a green light
$flock->lock(); // Lock right away
//DO STUFF;
$flock->unlock(); // Release the lock so others can access
$done = true; // Allows the loop to exit
} else if ($sleep++ > MAX_SLEEP) { // Giving up, cannot write
// Handle exception, there are many possibilities:
// Log exception and do nothing (definitely log)
// Force a write
// See if another process has been running for too long
// Check for timestamp of the lock file, maybe left behind after a reboot
} else {
sleep(SLEEP_TIME);
}
} while(! $done);