我有一个方法,我从twitter API加载特定id的关注者,我在课堂上使用它两次,一个加载logged in user's followers
,一个加载other user's followers
。登录用户的关注者工作正常,代码类似于下面,只是不同的ID。以下代码用于加载followers for other users
。我想是在方法中没有调用onResponse
,因此otherFollowers
为空。我能知道为什么吗?
public interface ServiceListeners2 {
//For getting friends : @GET("1.1/friends/list.json")
@GET("1.1/followers/list.json")
Call<FollowersResponseModel> list(@Query("user_id") long id);
}
栈跟踪
E/AndroidRuntime: FATAL EXCEPTION: main
Process: chuan.twittertwitterlittlestar, PID: 9957
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at chuan.twittertwitterlittlestar.FollowerFragment.loadOtherFollowers(FollowerFragment.java:256)
at chuan.twittertwitterlittlestar.FollowerFragment.access$100(FollowerFragment.java:50)
at chuan.twittertwitterlittlestar.FollowerFragment$3.onItemClick(FollowerFragment.java:175)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3134)
at android.widget.AbsListView$3.run(AbsListView.java:4049)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.
在onCreateView中,
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (twitterFollowers.get(position).getId() != 0){
follower_num = loadOtherFollowers(twitterFollowers.get(position).getId());
MyCustomAlertDialog(twitterFollowers.get(position).getProfilePictureUrl(), follower_num);
Toast.makeText(getActivity(), twitterFollowers.get(position).getScreenName(),Toast.LENGTH_LONG).show();
//sendMsg(twitterFriends.get(position).getId(),twitterFriends.get(position).getScreenName(),"");
}
}
});
答案 0 :(得分:0)
好的就是这件事。您正在尝试调用同步方法来返回在完成网络调用Twitter API时将创建的内容。但是在完成并获取结果之前,列表为空,因此应用程序将始终崩溃。这是你能做的。
创建一个侦听器接口,该接口将充当API中的数据与列表中的用法之间的桥梁,如下所示:
build job: 'debug/job2', parameters: [[$class: 'StringParameterValue', name: 'PARAM', value: env.VERSION]], wait: false
并在此之后修改此方法,如下所示:
interface MyListener {
void onFollowersReceived(int listSize);
}
然后在你的列表中它将是这样的:
private void loadOtherFollowers(long id, MyListener listener) {
Log.e("ID","id is "+id);
MyTwitterApiClient myTwitterApiClient2 = new MyTwitterApiClient(twitterSession);
myTwitterApiClient2.getCustomTwitterService2().list(id).enqueue(new retrofit2.Callback<FollowersResponseModel>() {
@Override
public void onResponse(Call<FollowersResponseModel> call, Response<FollowersResponseModel> response) {
Log.e("onResponse",response.toString());
otherFollowers = fetchResults2(response);
Log.e("onResponse","otherFollowers:" +otherFollowers.size());
listener.onFollowersReceived(otherFollowers.size());
}
@Override
public void onFailure(Call<FollowersResponseModel> call, Throwable t) {
Log.e("onFailure",t.toString());
listener.onFollowersReceived(0);// here you can also put -1 and handle it as error
}
});
}
希望它有所帮助!!
答案 1 :(得分:0)
试试这个:
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (twitterFollowers.get(position).getId() != 0){
loadOtherFollowers(twitterFollowers.get(position).getId());
}
}
});
private void loadOtherFollowers(long id) {
Log.e("ID","id is "+id);
MyTwitterApiClient myTwitterApiClient2 = new MyTwitterApiClient(twitterSession);
myTwitterApiClient2.getCustomTwitterService2().list(id).enqueue(new retrofit2.Callback<FollowersResponseModel>() {
@Override
public void onResponse(Call<FollowersResponseModel> call, Response<FollowersResponseModel> response) {
Log.e("onResponse",response.toString());
otherFollowers = fetchResults2(response);
Log.e("onResponse","otherFollowers:" +otherFollowers.size());
MyCustomAlertDialog(twitterFollowers.get(position).getProfilePictureUrl(), otherFollowers.size());
Toast.makeText(getActivity(), twitterFollowers.get(position).getScreenName(),Toast.LENGTH_LONG).show();
//sendMsg(twitterFriends.get(position).getId(),twitterFriends.get(position).getScreenName(),"");
}
@Override
public void onFailure(Call<FollowersResponseModel> call, Throwable t) {
Log.e("onFailure",t.toString());
}
});
}