我正在尝试实现一个连接到谷歌谈话服务的安卓聊天客户端。我已经完成了大部分工作,连接到服务器,获取联系人(并将它们显示在 ListView 中),并在单击发送和接收消息的列表上的联系人姓名时打开一个新活动。我的问题是,现在我正在尝试获取用户帐户联系人的存在。我已经编写了这段代码,但它不起作用,我无法找出原因。任何帮助表示赞赏。
Button connect = (Button) findViewById(R.id.connect);
connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String username = getTheText(R.id.UserName);
String password = getTheText(R.id.Password);
login(username, password);
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
roster = connection.getRoster();
entries = roster.getEntries();
arrayOfEntries = new ArrayList<RosterEntry>(entries);
searchedContacts = GetSearchContacts();\\This method is the one who is not working correctly
try{
launchNextActivity();
}catch(Exception e){
e.printStackTrace();
}
}
});
/*
* Asigna los datos del ArrayList de RosterEntry al ArraList de SearchContacts
*/
private ArrayList<SearchContacts> GetSearchContacts(){
ArrayList<SearchContacts> results = new ArrayList<SearchContacts>();
\\The class SearchContacts is an own class very simple with a constructor which assigns 4 parameters to the objects of the class, and getters and setters for every parameter.
for (RosterEntry r : arrayOfEntries){
SearchContacts sc = new SearchContacts(null, null,null,null);
sc.setNick(r.getName());
sc.setEmail(r.getUser());
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
prs = roster.getPresence(r.getUser());
Presence.Mode presence2 = prs.getMode();
status = ConvierteEnumToString(presence2);
sc.setFirstSentence(status);
results.add(sc);
}
return results;
}
\*
*
*\
private void ConvierteEnumToString(Presence.Mode pm){
switch(pm){
case available:
Log.i(LOGTAG,"Case Available");
status= "Available";
break;
case away:
Log.i(LOGTAG,"Case away");
status= "Away";
break;
case chat:
Log.i(LOGTAG,"Case chat");
status= "chat";
break;
case dnd:
Log.i(LOGTAG,"Case dnd");
status= "Do not disturb";
break;
case xa:
Log.i(LOGTAG,"Case xa");
status= "Unavailable";
break;
default:
Log.i(LOGTAG,"Invalid status");
status="Invalid";
break;
}
}
如果我更改此行的属性: String status = ConvierteEnumToString(presence2); 我直接输入 Presence.Mode.available,它将状态分配给我所有的联系人,但它工作正常。如果我让它在上面的代码中,我会得到一个这样的异常:
04-10 23:11:02.866: W/dalvikvm(1722): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-10 23:11:02.886: E/AndroidRuntime(1722): FATAL EXCEPTION: main
04-10 23:11:02.886: E/AndroidRuntime(1722): java.lang.NullPointerException
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.chat.Login.GetSearchContacts(Login.java:164)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.chat.Login.access$1(Login.java:153)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.chat.Login$1.onClick(Login.java:81)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.view.View.performClick(View.java:2485)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.view.View$PerformClick.run(View.java:9080)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.os.Handler.handleCallback(Handler.java:587)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.os.Looper.loop(Looper.java:123)
04-10 23:11:02.886: E/AndroidRuntime(1722): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-10 23:11:02.886: E/AndroidRuntime(1722): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 23:11:02.886: E/AndroidRuntime(1722): at java.lang.reflect.Method.invoke(Method.java:507)
04-10 23:11:02.886: E/AndroidRuntime(1722): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-10 23:11:02.886: E/AndroidRuntime(1722): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-10 23:11:02.886: E/AndroidRuntime(1722): at dalvik.system.NativeStart.main(Native Method)
不同的是,我有时会获得一个用户状态,然后发生异常,有时根本没有获得任何状态。非常感谢您的帮助!