我正在尝试从另一个在另一个线程上运行的类填充ListView,但我没有这样做。 这是主要活动:
public ListView myList;
List < string>contacts = new List< string>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Control);
myList = FindViewById<ListView>(Resource.Id.contactsList);
myList.ItemLongClick += MyList_ItemLongClick;
}
public void insertContact(string ContactInfo)
{
myList=FindViewById<ListView>(Resource.Id.contactsList);
contacts.Add(ContactInfo);
ArrayAdapter<string> myArray = new ArrayAdapter<string>(Application.Context, Android.Resource.Layout.SimpleListItem1, contacts);
list.Adapter = null;
list.Adapter = myArray;
myList.DeferNotifyDataSetChanged();
}
public void startAsync(){
Contacts con = new Contacts();
Thread thread = new Thread(()=> con.Start());
thread.Start();
}
我正在从另一个类触发一个方法来抓取联系人。 它只是得到一些信息,应该发送到主要活动 另一个类就像
MainAct main = new MainAct();
main.insertContact("new Contact");
但它没有做任何事。 我试图创建一个方法从主活动中获取listView(如某些线程中所述)并将其作为第二个参数传递给添加联系人,如:
public ListView getListView()
{
ListView myList = FindViewById<ListView>(Resource.Id.contactsList);
return myList;
}
然后调用具有ListView的这一行:
main.insertContact("new Contact", main.getListView());
但我有一个例外:
Java.Lang.NullPointerException: < Timeout exceeded getting exception details>
我尝试从另一个类运行这样的行:
main.RunOnUIThread(()=> main.insertContact("new Contact", main.getListView()));
但得到了同样的例外! 我错过了什么?
注意:如果我将两个类放在一个类中(我的意思是一个类中的所有代码)它对RunOnUIThread(()=&gt;)没有问题; 。但是当我将方法移动到另一个类时,我得到了异常。
编辑1: 当我发现异常时,我得到了nullpointer
Exception of type 'Java.Lang.NullPointerException' was thrown.
答案 0 :(得分:1)
1)如果Contacts
是您的另一个课程,我建议您将其重命名为MyContacts
,因为有一个Contacts课程,会让我们感到困惑。
2)您不需要新的MainAct
,只需将上下文传递给您的另一个班级。
我试图重现您的问题,并为这些代码添加注释:
这是您的MyContacts
班级:
public class MyContacts
{
MainActivity mContext;
public MyContacts(MainActivity context) {
this.mContext = context;
}
public void Start() {
mContext.insertContact("new Contact");
mContext.insertContact("new Contact1");
mContext.insertContact("new Contact2");
}
}
你MainActivity
:
public class MainActivity : Activity
{
public ListView myList;
List<string> contacts = new List<string>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
myList = FindViewById<ListView>(Resource.Id.contactsList);
//we usually init the data in OnCreate method.
startAsync();
}
public void insertContact(string ContactInfo)
{
//this is redundant, because you have Instantiated the myList in the OnCreate method.
//myList = FindViewById<ListView>(Resource.Id.contactsList);
contacts.Add(ContactInfo);
//You can use this instead of Application.Context.
ArrayAdapter<string> myArray = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, contacts);
myList.Adapter = myArray;
//from your question, I can't find any information about the list property.
//list.Adapter = null;
//list.Adapter = myArray;
//myList.DeferNotifyDataSetChanged();
}
//From your question, I can't find where have you call this method, I will call it in the OnCreate method
public void startAsync()
{
MyContacts con = new MyContacts(this);
Thread thread = new Thread(() => con.Start());
thread.Start();
}
}