package com.example.coder.thecompleteeduhub;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SplashScreen extends AppCompatActivity {
private ViewPager slideViewpage;
private LinearLayout linearLayout;
private SliderAdapter sliderAdapter;
private TextView[] dots;
private Button btnBack,btnNext;
private int mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
slideViewpage=findViewById(R.id.sliderViewPager);
linearLayout=findViewById(R.id.linearLayout);
sliderAdapter =new SliderAdapter(this);
btnBack=findViewById(R.id.btnBack);
btnNext=findViewById(R.id.btnNext);
slideViewpage.setAdapter(sliderAdapter);
addDotIndicator(0);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slideViewpage.setCurrentItem(mCurrentPage+1);
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slideViewpage.setCurrentItem(mCurrentPage-1);
}
});
}
public void addDotIndicator(int position)
{
dots=new TextView[5];
linearLayout.removeAllViews();
for (int i=0; i<=dots.length;i++)
{
dots[i]=new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(getResources().getColor(R.color.colorTransparentWhite));
linearLayout.addView(dots[i]);
slideViewpage.addOnPageChangeListener(viewListner);
}
if (dots.length>0)
{
dots[position].setTextColor(getResources().getColor(R.color.colorWhite));
}
}
ViewPager.OnPageChangeListener viewListner=new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
addDotIndicator(i);
mCurrentPage = i;
if (i==0)
{
btnNext.setEnabled(true);
btnBack.setEnabled(false);
btnBack.setVisibility(View.INVISIBLE);
btnNext.setText("Next");
btnBack.setText("");
}
else if (i==dots.length-1)
{
btnNext.setEnabled(true);
btnBack.setEnabled(true);
btnBack.setVisibility(View.VISIBLE);
btnNext.setText("Finish");
btnBack.setText("");
}
else
{
btnNext.setEnabled(true);
btnBack.setEnabled(true);
btnBack.setVisibility(View.VISIBLE);
btnNext.setText("Next");
btnBack.setText("Back");
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
};
}
package com.example.coder.thecompleteeduhub;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SplashScreen extends AppCompatActivity {
private ViewPager slideViewpage;
private LinearLayout linearLayout;
private SliderAdapter sliderAdapter;
private TextView[] dots;
private Button btnBack,btnNext;
private int mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
slideViewpage=findViewById(R.id.sliderViewPager);
linearLayout=findViewById(R.id.linearLayout);
sliderAdapter =new SliderAdapter(this);
btnBack=findViewById(R.id.btnBack);
btnNext=findViewById(R.id.btnNext);
slideViewpage.setAdapter(sliderAdapter);
addDotIndicator(0);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slideViewpage.setCurrentItem(mCurrentPage+1);
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slideViewpage.setCurrentItem(mCurrentPage-1);
}
});
}
public void addDotIndicator(int position)
{
dots=new TextView[5];
linearLayout.removeAllViews();
for (int i=0; i<=dots.length;i++)
{
dots[i]=new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(getResources().getColor(R.color.colorTransparentWhite));
linearLayout.addView(dots[i]);
slideViewpage.addOnPageChangeListener(viewListner);
}
if (dots.length>0)
{
dots[position].setTextColor(getResources().getColor(R.color.colorWhite));
}
}
ViewPager.OnPageChangeListener viewListner=new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
addDotIndicator(i);
mCurrentPage = i;
if (i==0)
{
btnNext.setEnabled(true);
btnBack.setEnabled(false);
btnBack.setVisibility(View.INVISIBLE);
btnNext.setText("Next");
btnBack.setText("");
}
else if (i==dots.length-1)
{
btnNext.setEnabled(true);
btnBack.setEnabled(true);
btnBack.setVisibility(View.VISIBLE);
btnNext.setText("Finish");
btnBack.setText("");
}
else
{
btnNext.setEnabled(true);
btnBack.setEnabled(true);
btnBack.setVisibility(View.VISIBLE);
btnNext.setText("Next");
btnBack.setText("Back");
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashScreen"
android:background="@drawable/main_background">
<android.support.v4.view.ViewPager
android:id="@+id/sliderViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="56dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="164dp"
android:layout_marginEnd="164dp"
android:layout_marginBottom="12dp"
android:gravity="center"
android:visibility="invisible"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout"
android:layout_alignParentStart="true"
android:layout_marginStart="18dp"
android:text="@string/btn_previous" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout"
android:layout_alignParentEnd="true"
android:layout_marginEnd="18dp"
android:text="@string/btn_next" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/imageViewSlide"
android:layout_width="163dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
app:srcCompat="@drawable/icons8_university_64" />
<TextView
android:id="@+id/slider_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="226dp"
android:text="@string/university_slider"
android:textSize="30dp" />
<TextView
android:id="@+id/slider_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="363dp"
android:text="@string/university_slider_detail"
android:textAlignment="center"
android:textSize="20sp" />
</RelativeLayout>
Process: com.example.coder.thecompleteeduhub, PID: 24169
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coder.thecompleteeduhub/com.example.coder.thecompleteeduhub.SplashScreen}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at com.example.coder.thecompleteeduhub.SplashScreen.addDotIndicator(SplashScreen.java:51)
at com.example.coder.thecompleteeduhub.SplashScreen.onCreate(SplashScreen.java:31)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-09 16:29:14.161 2640-24365/? E/android.os.Debug: ro.product_ship = true
10-09 16:29:14.161 2640-24365/? E/android.os.Debug: ro.debug_level = 0x4f4c
10-09 16:29:14.161 2640-24365/? E/android.os.Debug: sys.mobilecare.preload = false
10-09 16:29:14.851 24395-24395/? E/Zygote: v2
SliderScreen(MainScreen)的图像 SliderScreen Image SliderLayout Layout Design Image
我见过很多次,但仍未得到解决。请帮助找出错误。 我看到了ArrayBoundException错误,但我也看到了,但没有解决问题
答案 0 :(得分:1)
使用此for (int i=0; i<dots.length;i++)
代替for (int i=0; i<=dots.length;i++)