Мне нужно динамически изменить свой URL-адрес, чтобы приложение для Android делало нумерацию страниц с помощью Wordpress Rest API, оно работает и прекрасно загружается в моем приложении, но после загрузки последней страницы, когда count ++ равно нулю, просто прокручивая больше после сбоя приложения на последней страницекак вернуть нулевое значение, чтобы приложение не зависало.
count = 0;
public void getRetrofit(){
int numberPage = count++;
String url = "https://example.com/wp-json/wp/v2/posts?page="+numberPage;
Пожалуйста, помогите мне решить эту проблему для следующего кода Java.Я буду очень благодарен, если кто-нибудь поможет мне, потому что я расстраиваюсь из-за многих дней.Ниже мой следующий код Java.Я знаю, что должен вставить что-то вроде if (count ++ == null) {return;}, но я могу понять, как это сделать. Еще раз спасибо.
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private LinearLayoutManager mLayoutManager;
private ArrayList<Model> list;
private RecyclerViewAdapter adapter;
Boolean isScrolling = false;
int currentItems, totalItems, scrollOutItems;
int count = 1;
private String baseURL = "https://example.com";
public static List<WPPost> mListPost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
mLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mLayoutManager);
list = new ArrayList<Model>();
adapter = new RecyclerViewAdapter( list, MainActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
{
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentItems = mLayoutManager.getChildCount();
totalItems = mLayoutManager.getItemCount();
scrollOutItems = mLayoutManager.findFirstVisibleItemPosition();
if(isScrolling && (currentItems + scrollOutItems == totalItems))
{
isScrolling = false;
getRetrofit();
}
}
});
getRetrofit();
}
public void getRetrofit(){
int numberPage = count++;
String url = "https://example.com/wp-json/wp/v2/posts?page="+numberPage;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
Call<List<WPPost>> call = service.getPostInfo(url);
call.enqueue(new Callback<List<WPPost>>() {
@Override
public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) {
Log.e("mainactivyt", " response "+ response.body());
mListPost = response.body();
progressBar.setVisibility(View.GONE);
for (int i=0; i<response.body().size();i++){
list.addAll(list.getItems());
}
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<WPPost>> call, Throwable t) {
}
});
}
public static List<WPPost> getList(){
return mListPost;
}
}
Stack Trace
10-14 21:23:33.358 17119-17124/com.namansoftlabs.wordpressmymod I/zygote: Do full code cache collection, code=125KB, data=101KB
10-14 21:23:33.360 17119-17124/com.namansoftlabs.wordpressmymod I/zygote: After code cache collection, code=118KB, data=78KB
10-14 21:23:33.901 17119-17124/com.namansoftlabs.wordpressmymod I/zygote: Do partial code cache collection, code=122KB, data=91KB
10-14 21:23:33.902 17119-17124/com.namansoftlabs.wordpressmymod I/zygote: After code cache collection, code=122KB, data=91KB
Increasing code cache capacity to 512KB
10-14 21:23:36.324 17119-17119/com.namansoftlabs.wordpressmymod E/mainactivyt: response [com.namansoftlabs.wordpressmymod.WPPost@2863a0d]
10-14 21:23:36.324 17119-17119/com.namansoftlabs.wordpressmymod E/main: title 21 This is the Wp Sample Post 13
10-14 21:23:36.410 17119-17119/com.namansoftlabs.wordpressmymod E/mainactivyt: response [com.namansoftlabs.wordpressmymod.WPPost@a696a09, com.namansoftlabs.wordpressmymod.WPPost@927140e, com.namansoftlabs.wordpressmymod.WPPost@e93fe2f, com.namansoftlabs.wordpressmymod.WPPost@3cfbc3c, com.namansoftlabs.wordpressmymod.WPPost@d359dc5, com.namansoftlabs.wordpressmymod.WPPost@144631a, com.namansoftlabs.wordpressmymod.WPPost@6c4e4b, com.namansoftlabs.wordpressmymod.WPPost@6fa6028, com.namansoftlabs.wordpressmymod.WPPost@8d75141, com.namansoftlabs.wordpressmymod.WPPost@7b596e6]
10-14 21:23:36.410 17119-17119/com.namansoftlabs.wordpressmymod E/main: title 11 This is the Wp Sample Post 31
title 12 This is the Wp Sample Post 30
title 13 This is the Wp Sample Post 29
10-14 21:23:36.411 17119-17119/com.namansoftlabs.wordpressmymod E/main: title 14 This is the Wp Sample Post 28
title 15 This is the Wp Sample Post 21
title 16 This is the Wp Sample Post 20
title 17 This is the Wp Sample Post 19
10-14 21:23:36.412 17119-17119/com.namansoftlabs.wordpressmymod E/main: title 18 This is the Wp Sample Post 18
title 19 This is the Wp Sample Post 17
10-14 21:23:36.413 17119-17119/com.namansoftlabs.wordpressmymod E/main: title 20 This is the Wp Sample Post 16
10-14 21:23:39.474 17119-17124/com.namansoftlabs.wordpressmymod I/zygote: Do full code cache collection, code=252KB, data=208KB
10-14 21:23:39.478 17119-17124/com.namansoftlabs.wordpressmymod I/zygote: After code cache collection, code=220KB, data=156KB
10-14 21:23:41.213 17119-17119/com.namansoftlabs.wordpressmymod E/mainactivyt: response null
10-14 21:23:41.213 17119-17119/com.namansoftlabs.wordpressmymod D/AndroidRuntime: Shutting down VM
--------- beginning of crash
10-14 21:23:41.214 17119-17119/com.namansoftlabs.wordpressmymod E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namansoftlabs.wordpressmymod, PID: 17119
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.namansoftlabs.wordpressmymod.MainActivity$2.onResponse(MainActivity.java:193)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)