我只是在列表视图中只显示个人资料列表和名称时遇到问题。
我认为我很接近,这是我的代码和我到目前为止遇到的麻烦:
public class TestActivity extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ListView lv = (ListView) findViewById(R.id.listView);
...
// Check for an open session
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// Get the friendlist data;
requestFacebookFriends(session);
}
/*ListView lv = (ListView) findViewById(R.id.listView);
aAdpt = new FriendListAdapter(friendlist,this);
lv.setAdapter(aAdpt);*/
// If I do that I get an Nullpointer from the adapter because at this point the friendlist is null
}
private void requestFacebookFriends(final Session session) {
Request friendrequest = Request.newMyFriendsRequest(session,
new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users,
Response response) {
if(users != null){
for(int i=0; i < users.size();i++){
GraphUser user = users.get(i);
Friends friend = new Friends();
friend.setFriendsID(user.getId());
friend.setFriendsUserNameView(user.getName());
//friend.setFriendsProfilePictureView(user.getId());
friendlist.add(friend);
Log.d(TAG,"User ID "+ user.getName());
}
}
}
});
friendrequest.executeAsync();
}
所以我想我在这里做错了,我也尝试在循环后从onCompleted方法启动适配器,但是我无法使它工作。
有任何线索吗? 如果需要,我会用我的适配器更新我的诗句,但我不认为问题来自那里。
编辑: logcat的:
01-11 04:03:19.848: E/AndroidRuntime(6653): FATAL EXCEPTION: main
01-11 04:03:19.848: E/AndroidRuntime(6653): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.TestActivity}: java.lang.NullPointerException
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.os.Looper.loop(Looper.java:137)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-11 04:03:19.848: E/AndroidRuntime(6653): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 04:03:19.848: E/AndroidRuntime(6653): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 04:03:19.848: E/AndroidRuntime(6653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-11 04:03:19.848: E/AndroidRuntime(6653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-11 04:03:19.848: E/AndroidRuntime(6653): at dalvik.system.NativeStart.main(Native Method)
01-11 04:03:19.848: E/AndroidRuntime(6653): Caused by: java.lang.NullPointerException
01-11 04:03:19.848: E/AndroidRuntime(6653): at com.example.myfirstapp.FriendListAdapter.getCount(FriendListAdapter.java:26)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.widget.ListView.setAdapter(ListView.java:460)
01-11 04:03:19.848: E/AndroidRuntime(6653): at com.example.myfirstapp.TestActivity.onCreate(TestActivity.java:69)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.Activity.performCreate(Activity.java:4465)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-11 04:03:19.848: E/AndroidRuntime(6653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-11 04:03:19.848: E/AndroidRuntime(6653): ... 11 more
但正如我之前所说,我知道错误是由于我的朋友列表在我调用适配器时为空。