gpt4 book ai didi

Android listview空指针异常?

转载 作者:行者123 更新时间:2023-11-30 02:59:33 25 4
gpt4 key购买 nike

我正在制作一个聊天应用程序。,通过使用“xmpp 服务器”。在我的应用程序中。我正在使用一个 ListView ,其中显示了所有联系人的列表。,我正在尝试处理线程中的花名册值,而不使用线程并处理它的工作完美...,但是当

我在 Thread 中使用我的 listdata..,它给我“空指针异常”,我在做什么..我很困惑..,请帮帮我..,这是我的代码

public class Contact_ListActivity extends Activity  {
TextView tv_header;
TextView tv_account;
TextView tv_contact;
TextView tv_number;
EditText idnumber;
Button add;
public ListView list_view;
XMPPObjectPass commanInstant;

public ArrayList<ProfileInfo> list = new ArrayList<ProfileInfo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);

commanInstant = (XMPPObjectPass)getApplicationContext();

tv_account=(TextView)findViewById(R.id.tv_account);
tv_contact=(TextView)findViewById(R.id.tv_contact);
tv_number=(TextView)findViewById(R.id.tv_number);
tv_header=(TextView)findViewById(R.id.tv_header);
idnumber=(EditText)findViewById(R.id.idnumber);

add=(Button)findViewById(R.id.add);
list_view=(ListView)findViewById(R.id.listview);

list_view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Object o = list_view.getItemAtPosition(position);
// list_activity_bean obj_itemDetails = (list_activity_bean)o;
// Toast.makeText(Contact_ListActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
tv_account.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
startActivity(i);
}
});
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
list.clear();
friendslist();
list_view.setAdapter(new ListActivityAdapter(this, list));
}

public void friendslist()
{
Thread coThread = new Thread();
{
new Runnable() {
public void run() {

try
{

Roster roster = commanInstant.connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
tv_number.setText(""+entries.size());
for(RosterEntry r:entries)
{
VCard card = new VCard();
try {
card.load(commanInstant.connection,r.getUser());
System.out.println("<-- first anme " + card.getFirstName());

//card.get
// System.out.println("<-- getEmailHome anme " + card.getEmailHome());
// System.out.println("<-- getEmailWork anme " + card.getEmailWork());
// System.out.println("<-- getFrom anme " + card.getFrom());
// System.out.println("<-- getJabberId anme " + card.getJabberId());
// System.out.println("<-- getLastName anme " + card.getLastName());
// System.out.println("<-- getMiddleName anme " + card.getMiddleName());
// System.out.println("<-- getNickName anme " + card.getNickName());
// System.out.println("<-- getOrganization anme " + card.getOrganization());

}
catch (Exception e)
{
e.printStackTrace();
}


byte[] imgs = card.getAvatar();
if (imgs != null) {
int len = imgs.length;
Bitmap img = BitmapFactory.decodeByteArray(imgs, 0, len);

list.add(new ProfileInfo( card.getFirstName(),img,"+1PTS"));
System.out.println(r);

}
}

}
catch (Exception e)
{
e.printStackTrace();
System.out.println(""+e);
}
}
};
coThread.start();
}
}
}

提前致谢..:)

最佳答案

试试这个...,它会工作...

public class Contact_ListActivity extends Activity  {
TextView tv_header;
TextView tv_account;
TextView tv_contact;
TextView tv_number;
EditText idnumber;
Button add;
public ListView list_view;
XMPPObjectPass commanInstant;
Handler mHandler;
public ArrayList<ProfileInfo> list = new ArrayList<ProfileInfo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);

commanInstant = (XMPPObjectPass)getApplicationContext();

tv_account=(TextView)findViewById(R.id.tv_account);
tv_contact=(TextView)findViewById(R.id.tv_contact);
tv_number=(TextView)findViewById(R.id.tv_number);
tv_header=(TextView)findViewById(R.id.tv_header);
idnumber=(EditText)findViewById(R.id.idnumber);

add=(Button)findViewById(R.id.add);
list_view=(ListView)findViewById(R.id.listview);

list_view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Object o = list_view.getItemAtPosition(position);
// list_activity_bean obj_itemDetails = (list_activity_bean)o;
// Toast.makeText(Contact_ListActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
tv_account.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
startActivity(i);
}
});
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
list.clear();
friendslist();
list_view.setAdapter(new ListActivityAdapter(this, list));
}

public void friendslist()
{
mHandler = new Handler();
Thread coThread = new Thread();
{

mHandler.post(new Runnable() {
@Override
public void run() {

try
{

Roster roster = commanInstant.connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
tv_number.setText(""+entries.size());
for(RosterEntry r:entries)
{
VCard card = new VCard();
try {
card.load(commanInstant.connection,r.getUser());
System.out.println("<-- first anme " + card.getFirstName());

//card.get
// System.out.println("<-- getEmailHome anme " + card.getEmailHome());
// System.out.println("<-- getEmailWork anme " + card.getEmailWork());
// System.out.println("<-- getFrom anme " + card.getFrom());
// System.out.println("<-- getJabberId anme " + card.getJabberId());
// System.out.println("<-- getLastName anme " + card.getLastName());
// System.out.println("<-- getMiddleName anme " + card.getMiddleName());
// System.out.println("<-- getNickName anme " + card.getNickName());
// System.out.println("<-- getOrganization anme " + card.getOrganization());

}
catch (Exception e)
{
e.printStackTrace();
}


byte[] imgs = card.getAvatar();
if (imgs != null) {
int len = imgs.length;
Bitmap img = BitmapFactory.decodeByteArray(imgs, 0, len);

list.add(new ProfileInfo( card.getFirstName(),img,"+1PTS"));
System.out.println(r);

}
}

}
catch (Exception e)
{
e.printStackTrace();
System.out.println(""+e);
}
}
});
coThread.start();
}
}
}

关于Android listview空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com