我动态地实现“简介”屏幕,这是首次启动应用。我通过api调用从服务器获取图像,从服务器获取5张图像并加载到 viewpager 中。 滑动到第五张图片时,应用崩溃并显示以下错误 这是我的代码,
public class IntroScreenActivity extends AppCompatActivity {
@BindView(R.id.view_pager)
ViewPager mviewPager;
@BindView(R.id.layoutDots)
LinearLayout mLayoutDots;
@BindView(R.id.skip)
TextView mSkip;
@BindView(R.id.next)
TextView mNext;
@BindView(R.id.progressBar)
ProgressBar mProgressBar;
private TextView[] dots;
private SliderSharedPreferences sliderSharedPreferences;
private ArrayList<String> introImageSlidersList;
private int currentItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introscreen);
ButterKnife.bind(this);
introImageSlidersList = new ArrayList<>();
getIntroScreenImages();
// Checking for first time launch - before calling setContentView()
sliderSharedPreferences = new SliderSharedPreferences(this);
if (!sliderSharedPreferences.isFirstTimeLaunch()) {
launchHomeScreen();
finish();
}
init();
// adding bottom dots
addBottomDots(0);
mSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchHomeScreen();
}
});
mNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checking for last page
// if last page home screen will be launched
if (currentItem < introImageSlidersList.size() - 1) {
++currentItem; // increase the value by 1
mviewPager.setCurrentItem(currentItem); // set the layout at next position
} else {
launchHomeScreen();
}
}
});
}
private void init() {
for (int i = 0; i < introImageSlidersList.size(); i++)
mviewPager.setAdapter(new SlidingImage_Adapter(this, introImageSlidersList));
// Pager listener over indicator
mviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentItem = position; // currentItem used to get current position and then increase position on click on next button
addBottomDots(position);
if (position == introImageSlidersList.size() - 1) {
// last page. make button text to GOT IT
mSkip.setVisibility(View.GONE);
} else {
}
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}
private int getItem(int i) {
return mviewPager.getCurrentItem() + i;
}
public class SlidingImage_Adapter extends PagerAdapter {
private ArrayList<String> imageList;
private LayoutInflater inflater;
private Context context;
public SlidingImage_Adapter(Context context, ArrayList<String> imageList) {
this.context = context;
this.imageList = imageList;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.intro_slides, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
Picasso.with(context).load(PawParentConstants.BASE_URL + introImageSlidersList.get(position)).fit().centerCrop().placeholder(R.drawable.pawsanta_logo).into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
private void addBottomDots(int currentPage) {
dots = new TextView[introImageSlidersList.size()];
int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);
Log.e("dots", String.valueOf(dots.length));
Log.e("introImageSlidersList", String.valueOf(introImageSlidersList.size()));
mLayoutDots.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
Log.e("currentPage", String.valueOf(currentPage));
dots[i].setTextColor(colorsInactive[currentPage]);
mLayoutDots.addView(dots[i]);
}
if (dots.length > 0)
dots[currentPage].setTextColor(colorsActive[currentPage]);
}
private void launchHomeScreen() {
Toast.makeText(getActivity(), "Launch home screen, Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed() {
finish();
finishAffinity();
}
private void getIntroScreenImages() {
if (NetworkConnectionMonitor.getInstance().isNetworkConnected()) {
Call<JsonObject> call = PawParentConnect.getInstance(IntroScreenActivity.this).getApi().getIntroScreenSlider();
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(@NonNull Call<JsonObject> call, @NonNull Response<JsonObject> response) {
if (response.isSuccessful() && response.body() != null) {
try {
JsonObject jsonObject = response.body().getAsJsonObject();
String status = jsonObject.get("status").getAsString();
switch (status) {
case "Success":
JsonArray dataArray = jsonObject.getAsJsonArray("data");
if (dataArray != null && dataArray.size() > 0) {
Type listType = new TypeToken<List<JsonObject>>() {
}.getType();
final List<JsonObject> imageObjects = new Gson().fromJson(dataArray, listType);
for (int i = 0; i < dataArray.size(); i++) {
introImageSlidersList.add(imageObjects.get(i).get("path").getAsString());
}
init();
addBottomDots(0);
} else {
}
break;
case "Failure":
Toast.makeText(IntroScreenActivity.this, R.string.try_again, Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(IntroScreenActivity.this, R.string.try_again, Toast.LENGTH_SHORT).show();
break;
}
} catch (Exception e) {
//Toast.makeText(getActivity(), R.string.try_again, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(@NonNull Call<JsonObject> call, @NonNull Throwable t) {
Toast.makeText(IntroScreenActivity.this, R.string.try_again, Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(IntroScreenActivity.this, R.string.check_internet, Toast.LENGTH_SHORT).show();
}
}
}
java.lang.ArrayIndexOutOfBoundsException:length = 4;指数= 4 在com.slider.view.splash.IntroScreenActivity.addBottomDots(IntroScreenActivity.java:222) 在com.slider.view.splash.IntroScreenActivity.access $ 300(IntroScreenActivity.java:46) 在com.slider.view.splash.IntroScreenActivity $ 3.onPageSelected(IntroScreenActivity.java:128)