我正在开发带有图像滑块的android应用程序,从android中的mysql获取图像。我有一个带有字符串数组的图像滑块代码,其中包含一些图片的地址。这些图片显示良好,但是当我用凌空从服务器上获取图像时,我将获取的地址转换为另一个字符串数组,并像通过适配器中的第一个字符串数组一样传递了它,我的应用程序崩溃了。
Android
public class View_Room_Detail extends AppCompatActivity {
LinearLayout dots_layout;
TextView tvRoomType,tvRoomHonorName,tvPrice,tvGenralLocation,tvLocation;
ImageView[] dots;
int roomId;
String [] arr=new String[]{};
ArrayList<RoomGetterSetter> listPro=new ArrayList<RoomGetterSetter>();
//ViewFlipper vFlipper;
private String[] imageUrls = new String[]{
"https://cdn.pixabay.com/photo/2016/11/11/23/34/cat-1817970_960_720.jpg",
"https://cdn.pixabay.com/photo/2017/12/21/12/26/glowworm-3031704_960_720.jpg",
"https://cdn.pixabay.com/photo/2017/12/24/09/09/road-3036620_960_720.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_room_detail);
dots_layout=(LinearLayout) findViewById(R.id.dotsLayout);
tvRoomType=(TextView)findViewById(R.id.typeRoom);
tvRoomHonorName=(TextView)findViewById(R.id.honnorName);
tvPrice=(TextView)findViewById(R.id.priceRoom);
tvGenralLocation=(TextView)findViewById(R.id.genralLocation);
tvRoomType.setText(getIntent().getStringExtra("RoomNameIs"));
tvRoomHonorName.setText(getIntent().getStringExtra("HonorNameIs"));
tvPrice.setText(getIntent().getStringExtra("PriceIs"));
tvGenralLocation.setText(getIntent().getStringExtra("genrallocation"));
roomId=getIntent().getIntExtra("RoomIdIs",00000);
fetchImagesForImageSlider(roomId);
//getIntent().getStringExtra("TeacherNameIs")
ViewPager viewPager = findViewById(R.id.view_pager);
//////////////////////////////////////////////
StringRequest request = new StringRequest(Request.Method.POST, Constants.URL_GET_PIC_ROOOM_DETAILS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//dialog.dismiss();
try {
//Toast.makeText(View_Room_Detail.this, response, Toast.LENGTH_LONG).show();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray= jsonObject.getJSONArray("server_response");
for (int i=0;i<jsonArray.length();i++)
{
JSONObject object=jsonArray.getJSONObject(i);
// imageUrls=object.getString("image_url");
RoomGetterSetter subAttendance=new RoomGetterSetter(Constants.URL_GET_ROOM_PICTURE+object.getString("image_url"));
Toast.makeText(View_Room_Detail.this, "Response is:"+Constants.URL_GET_ROOM_PICTURE+object.getString("image_url"), Toast.LENGTH_SHORT).show();
listPro.add(subAttendance);
arr=new String[]{Constants.URL_GET_ROOM_PICTURE+object.getString("image_url"),
Constants.URL_GET_ROOM_PICTURE+object.getString("image_url"),
Constants.URL_GET_ROOM_PICTURE+object.getString("image_url"),
Constants.URL_GET_ROOM_PICTURE+object.getString("image_url")};
// System.out.println("array is:"+arr);
//imageUrls=arr.clone();
for (String pro:arr)
{
System.out.println("Members is:"+pro);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(View_Room_Detail.this, "Error is :"+error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> prams = new HashMap<>();
prams.put("id", String.valueOf(roomId));
return prams;
}
};
RequestHandler.getInstance(View_Room_Detail.this).addToRequestQueue(request);
//////////////////////////////////////////
//createDots(0);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
IndicatorView indicatorView=(IndicatorView)findViewById(R.id.inidicatorView);
indicatorView.attachViewPager(viewPager);
}
}
第一个字符串被拒绝为
私有字符串[] imageUrls =新字符串[] { “ https://cdn.pixabay.com/photo/2016/11/11/23/34/cat-1817970_960_720.jpg”, “ https://cdn.pixabay.com/photo/2017/12/21/12/26/glowworm-3031704_960_720.jpg”, “ https://cdn.pixabay.com/photo/2017/12/24/09/09/road-3036620_960_720.jpg” };
第二个数组是:
String [] arr = new String [] {}; 填写了OnResponse方法。
我打印了这个数组,它给了我正确的图像URL,但是当我在适配器中传递它时崩溃了。
ViewPagerAdapter adapter = new ViewPagerAdapter(this, arr);
viewPager.setAdapter(adapter);
Here is the link that i am following
Logcat错误是:
01-15 12:42:17.863 17400-17400/com.example.madnanijaz.studentcouchingcenter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.madnanijaz.studentcouchingcenter, PID: 17400
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at steelkiwi.com.library.view.IndicatorView.getDrawable(IndicatorView.java:465)
at steelkiwi.com.library.view.IndicatorView.setIndicatorSelectColor(IndicatorView.java:477)
at steelkiwi.com.library.view.IndicatorView.prepareDefaultTypeForShow(IndicatorView.java:189)
at steelkiwi.com.library.view.IndicatorView.access$000(IndicatorView.java:37)
at steelkiwi.com.library.view.IndicatorView$1.onGlobalLayout(IndicatorView.java:174)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:982)
答案 0 :(得分:0)
我得到了答案。没有将arr数组声明为正确。应该是:
String [] arr;
代替:
String [] arr=new String[]{};