数组超出边界索引(无法启动活动)



在运行应用程序时,它显示错误为"无法启动活动.. "ComponenInfo",长度=64,索引=64,应用程序崩溃。有什么解决办法吗?

包com.mayank.app.quizoclash;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class game extends AppCompatActivity implements View.OnClickListener {
private TextView ques, score, quesno,time;
    Button opt1, opt2, opt3, opt4,image;
    String phone,phone1,name,college,regno;
    int count1=0;
 int count=0;
    int i=0,j=0,k=1;
    private  String[] question={"What was Slade Wilson's alter ego?","What is the name of this episode?","Which Dire-wolf belonged to Robb?","What is the color of the couch in the coffee shop in F.R.I.E.N.D.S?","How does Sherlock first have his coffee?","Who told the Flash to wear a mask or suit?","What is Kristy Stapleton's rabbit called?",
            "What was the name of Monica's Boyfriend who was an alcoholic","What are Daenerys Dragons called","Phoebe changes her name to?","In How I Met Your mother, What is Robin's occupation?","How many friends does Sheldon have on MySpace?(BBT)","Rachel gets a tattoo. What is it?","Which man did this woman go on a date with?",
            "In the series "Arrow," who is character Oliver Queen's best friend?","What two characters featured prominently in the TV series did not appeear in the comics version?(The Walking Dead)","This police inspector regularly calls on Holmes to help solve difficult cases.","In what drug (now illegal) was Holmes known to imbibe, from time to time.",
            "Who does Sheldon believe has an "unresolved Oedipus complex"?(BBT)","What card did Joey think was stolen by the guy who robbed the apartment?","What is "Arrow" character John Diggle's profession?","What is the setting for Holmes and Moriarty's final, fatal encounter?","What instrument does Leonard play?(BBT)","How many blocks was Rachel's childhood dog LaPooh dragged?",
            "What was T-Dog's real name?(The Walking Dead)","What is Daredevil's alter ego?","What phrase did the Arrow use when he questioned someone on his father's list?","In Season 2, who is the manager of Central Perk?(Friends)","Where did Amy go to college?(BBT)","How did Holmes describe himself professionally?","Unlike many superheroes, Daredevil is deeply religious. What is his faith?",
            "Which girl did this guy date?","What was the pendant on the necklace Andrea planned to give Amy for her birthday?","What catastrophic event was the Undertaking, that destroyed a large part of The Glades?","What was the London address of the famous sleuth and his faithful companion, Dr. Watson?","Halfway through the movie, Colin Farrell is introduced as Kingpin's assassin of choice. What is the name of his character, who "can turn anything into a weapon"?","In "The One With the Lottery," what is the power ball number?","What does CDC Dr. Edwin Jenner liken the zombie virus to?",
            "Barry served as best man at whose wedding?","Who accompanies Sheldon and Amy on their first dinner date?(BBT)","Who turned out to be Thea's biological father, who saved her from one of Slade's superhuman soldiers?","Barry's longtime girlfriend was Iris West. What century is she from?","'Suits' is set in a law office. Can you name it?","After they've fallen in love, Elektra gets Daredevil's alter ego a ticket to a ball. He goes, accompanied by his partner at work. What does Elektra wear?","Phoebe's brother let her name one of his triplets. What did she name her?",
            "In Sherlock, Benedict Cumberbatch is Sherlock Holmes but who plays Doctor Watson?","How many dragons did Daenerys Targaryen originally have in The Game of Thrones?","What does VALAR MORGHULIS signify?","What was the name of the witch burned alive by Danerys on the funeral pyre of Khal Dargo?","In Breaking Badwhat was Walter White baby daughter's first name?","Sybil Buchanan is a character from which of these US TV series?","Margaret Schroeder is a character from which TV series","What is Monica's last name in tv series Friends?","What name is given to the seat of House Baratheon? ","Heisenberg?","An FX original, each season has a new story but some of same actors?","News program featuring real life cases?","Travel Chanel-Zak,Aaron,Billy,Jay and spirits?","Spin off from The Walking Dead?"
            ,"The Braverman's were the family on what drama series?","The Ghost Adventures Crew are originally from what city?","Sunday night AMC,- Rick, Daryl, Glen?","General Hospital takes place in what city?","Damon,Stefon,Elena,Bonnie and Caroline"};
    MyCountDownTimer countDownTimer = new MyCountDownTimer(12000 /* 12 Sec */,
            1000);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        Toast.makeText(this," fgd",Toast.LENGTH_SHORT).show();
        Intent i = getIntent();
        phone = i.getStringExtra("phone");
        phone1 = i.getStringExtra("phone1");
        name = i.getStringExtra("name");
        college = i.getStringExtra("college");
        regno = i.getStringExtra("regno");
        countDownTimer.start();
        toolbar.setTitle("TV Series");
        setSupportActionBar(toolbar);
        opt1=(Button)findViewById(R.id.opt1);
        count1=0;
        opt2=(Button)findViewById(R.id.opt2);
        opt3=(Button)findViewById(R.id.opt3);
        opt4=(Button)findViewById(R.id.opt4);
        time = (TextView) findViewById(R.id.time);
        opt1.setOnClickListener(this);
        opt2.setOnClickListener(this);
        opt3.setOnClickListener(this);
        opt4.setOnClickListener(this);
        ques=(TextView)findViewById(R.id.ques);
        quesno=(TextView)findViewById(R.id.quesno);
        Toast.makeText(this," fgd",Toast.LENGTH_SHORT).show();
        shuffleArray1(question, (Gamesupport2.option1), (Gamesupport2.option2), (Gamesupport2.option3), (Gamesupport2.option4));

            ques.setText(question[0]);
        opt1.setText(Gamesupport.option1[0]);
        opt2.setText(Gamesupport.option2[0]);
        opt3.setText(Gamesupport.option3[0]);
        opt4.setText(Gamesupport.option4[0]);

    }

    @Override
    public void onClick(View v) {
        if(v==opt2)
        {
            ++count;
            next();
        }
        else
            next();
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    static void shuffleArray1(String[] q, String[] option1, String[] option2, String[] option3, String[] option4) {

        // If running on Java 6 or older, use `new Random()` on RHS here
        Random rnd = ThreadLocalRandom.current();
        for (int i = q.length; i > 0; i--)
        {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            String a = q[index];
            q[index] = q[i];
            q[i] = a;
            String e = option1[index];
            option1[index] = option1[i];
            option1[i] = e;
            String b = option2[index];
            option2[index] = option2[i];
            option2[i] = b;
            String c = option3[index];
            option3[index] = option3[i];
            option3[i] = c;
            String d = option4[index];
            option4[index] = option4[i];
            option4[i] = d;
        }

    }
    private void next() {
        for(i=i+1;i<=question.length;i++,j++)
        {
        count1++;
            countDownTimer.start();
            if(count1==64)
            {
                countDownTimer.cancel();
                String str=""+count;
                Intent i = new Intent(this, Score.class);
                i.putExtra("phone", phone);
                i.putExtra("score", str);
                i.putExtra("name",name);
                i.putExtra("phone1",phone1);
                i.putExtra("college",college);
                i.putExtra("regno", regno);
                startActivity(i);
                break;
            }

            ques.setText(question[i]);
            opt1.setText(Gamesupport.option1[i]);
            opt2.setText(Gamesupport.option2[i]);
            opt3.setText(Gamesupport.option3[i]);
            opt4.setText(Gamesupport.option4[i]);
            if(count1==1||count1==2 || count1==3 || count1==4|| count1==5||count1==6||count1==7||count1==8||count1==9||count1==10||count1==11||count1==12||count1==13||count1==14||count1==15||count1==16 || count1==17 || count1==18|| count1==19||count1==20||count1==21||count1==22||count1==23||count1==24||count1==25||count1==26||count1==27||count1==28||count1==29||count1==30||count1==31||count1==32||count1==33||count1==34||count1==35||count1==36||count1==37||count1==38||count1==39||count1==40||count1==41||count1==42||count1==43||count1==44||count1==45||count1==46 || count1==47 || count1==48|| count1==49||count1==50||count1==51||count1==52||count1==53||count1==54||count1==55||count1==56||count1==57||count1==58||count1==59||count1==60 || count1==61 || count1==62||count1==63||count1==64||count1==65)
            {                quesno.setText("Question No :" + (i + 1));

                break;
            }
        }

    }

}

Java中的数组从0开始索引,在下面的循环中,您将从64倒数到1:

for (int i = q.length; i > 0; i--)

并且您正在尝试访问下面几行不存在的项目:

q[i]

你必须把你的循环改为:

for (int i = q.length - 1; i >= 0; i--)

最新更新