尝试使用View pager将服务器映像加载到滑块时出现错误。
这是我的代码:
private ArrayList<ImageModel> GetImages(final String business_id){
final ArrayList<ImageModel> list = new ArrayList<>();
final Serverurl serverUrl = new Serverurl();
serverUrl.url("Registration","showOtherImages");
StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, serverUrl.domain_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray jsonArray = obj.getJSONArray("images");
if (jsonArray.length() == 0) {
//Toast.makeText(MySlider.this, "Hello im blank", Toast.LENGTH_SHORT).show();
}
else{
for(int i = 0 ;i < 4; i++)
{
ImageModel imageModel = new ImageModel();
imageModel.setImage_dynamic(serverUrl.other_image + jsonArray.getJSONObject(i).getString("image_path"));
list.add(imageModel);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(Imageslider.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("business_id",business_id);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(Imageslider.this);
requestQueue.add(stringRequest);
return list;
}
private void init() {
mPager = findViewById(R.id.pager);
mPager.setAdapter(new ImageSliderAdapterDynamix(Imageslider.this,imageModelArrayList));
CirclePageIndicator indicator =
findViewById(R.id.indicator);
indicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
//Set circle indicator radius
indicator.setRadius(5 * density);
NUM_PAGES =imageModelArrayList.size();
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 3000, 3000);
// Pager listener over indicator
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}
以下是日志错误屏幕截图:
06-20 02:50:09.232 18880-18880 / com.example.aamer.aurangabad E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.example.aamer.aurangabad,PID:18880 java.lang.IllegalStateException:应用程序的PagerAdapter更改了适配器的内容,而没有调用PagerAdapter#notifyDataSetChanged!预期的适配器项数:0,找到的:4寻呼机ID:com.example.aamer.aurangabad:id / pager寻呼机类:类android.support.v4.view.ViewPager有问题的适配器:类adapters.ImageSliderAdapterDynamix 在android.support.v4.view.ViewPager.populate(ViewPager.java:1135) 在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662) 在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624) 在android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:616) 在com.example.aamer.aurangabad.Imageslider $ 4.run(Imageslider.java:139) 在android.os.Handler.handleCallback(Handler.java:790) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6501) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)