简体   繁体   中英

App keeps crashing when ListView item is clicked on

Hello I am new to Java and coding in general.

I am using a ListView to display different sets of words which are stored in array. Clicking an item in the list will display the words in the array according to which item has been clicked. I have added the lines of the code that display the text and display a hint into an if statement and now my app keeps crashing when I click on the item 0.

Could someone please give me some advice?

Here is my code :

package com.example.anotherapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    int i = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chooseGame();
    }

    public void chooseGame() {
        final ArrayList<String> arrayList = new ArrayList<String>();
        final TextView wordTextView = findViewById(R.id.wordTextView);
        final EditText editTextView = findViewById(R.id.enterEditText);
        final Button nextButton = findViewById(R.id.nextButton);
        ArrayList<String> gamesArrayList = new ArrayList<String>();
        gamesArrayList.add("A Vegan's Worst Nightmare");
        gamesArrayList.add("The Wet Floor Sign");
        gamesArrayList.add("The Meaning of life");
        gamesArrayList.add("Campfire Story");
        gamesArrayList.add("The Crocobearamouse");
        final ListView gamesListView = findViewById(R.id.gamesListView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                gamesArrayList);
        gamesListView.setAdapter(arrayAdapter);
        gamesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                 @Override
                                                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                     if (position == 0) {

                                                         gamesListView.setVisibility(View.INVISIBLE);
                                                         editTextView.setVisibility((View.VISIBLE));
                                                         wordTextView.setVisibility((View.VISIBLE));
                                                         nextButton.setVisibility(View.VISIBLE);


                                                         String[] zeroArray = {"Food", "Adjective", "Proper Noun", "Name"};
                                                         String displayHint = "";
                                                         String displayText = "";

                                                         displayText = enterWord() + zeroArray[i];
                                                         displayHint = zeroArray[i];

                                                         wordTextView.setText(displayText);
                                                         editTextView.setHint(displayHint);
                                                     }
                                                 }
                                             }
        );
    }

    public String enterWord() {
        String[] zeroArray = {"Food", "Adjective", "Proper Noun", "Name"};
        String entry;
        if (zeroArray[i].equals("Adjective")) {
            entry = "Enter an ";
        } else {
            entry = "Enter a ";
        }
        return entry;
    }

    public void nextWord(View view) {
        i++;
    }
}

您的变量i-1开始,您的代码调用array[i] ,我认为这是使您的应用程序崩溃的主要原因

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM