简体   繁体   English

启动活动时崩溃

[英]Crashing when launching activity

I am trying to start an Activity from the onPostExecute() method in this class: 我试图从此类中的onPostExecute()方法启动一个Activity:

private class LoadFragmentTask extends AsyncTask<Void, Void, Void> {


    private View view;
    private Bundle bundle;

    public LoadFragmentTask(View view){
        this.view = view;
    }

    @Override
    protected void onPreExecute(){
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading...");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        Dictionary dictionary = new com.example.james.ultimatescrabbleapp.Dictionary();
        final DatabaseHandler database = new DatabaseHandler(getActivity().getBaseContext());
        dictionary.linkDatabase(database);
        dictionary.setWordList();
        bundle = new Bundle();
        bundle.putSerializable("Dictionary", dictionary);


        return null;
    }

    @Override
    protected void onPostExecute(Void result){
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
            progressDialog = null;
        }

        Intent intent = new Intent(context, WordFinderActivity.class);
        intent.putExtra("Bundle", bundle);
        context.startActivity(intent);
    }
}

}

But whenever it tries to launch the activity, it crashes suddenly with no error, but I did manage to find an error somewhere that said Dictionary was not serializable, but it is: 但是,每当它尝试启动活动时,它都会突然崩溃而没有任何错误,但是我确实设法在某个地方发现了一个错误,该错误说字典无法序列化,但是它是:

public class Dictionary implements Serializable {
    private ArrayList<Word> words;
    public DatabaseHandler database;
/**'
 * Creates a new Dictionary object with the list of words from the text file (from the database.txt once done transferring)
 * @throws IOException
 */
public Dictionary() {

}

/**
 * Returns the list of words
 * @return the list of words
 */
public ArrayList<Word> getWordList() {
    return this.words;
}

/**
 * Returns the word at the specified index in the dictionary
 * @param index the index of the dictionary
 * @return the word at the specified index
 */
public String getWordAtIndex(int index) {
    Object[] wordArray = this.words.toArray();

    return wordArray[index - 1].toString();
}

/**
 * Returns the base word score for the specified word
 * @param word the word to get the base word score for
 * @return the base word score for the specified word
 */
public int getBaseWordScore(String word) {
    int totalScore = 0;
    String[] letters = word.split("");

    for (String letter : letters) {
        if (!letter.equals("")) {
            int letterScore;
            letterScore = this.getLetterScore(letter);
            totalScore += letterScore;
        }
    }

    return totalScore;
}

public boolean isWordOfficial(String word){
    return this.database.getWord(word).isWordOfficial();
}

/**
 * Returns the letter score for the specified letter
 * @param letter the letter to get the letter score for
 * @return the letter score for the specified letter
 */
public int getLetterScore(String letter) {
    int score = 0;

    if("eaionrtlsu".contains(letter)){
        score = 1;
    } else if("dg".contains(letter)){
        score = 2;
    } else if("bcmp".contains(letter)){
        score = 3;
    } else if("fhvwy".contains(letter)){
        score = 4;
    } else if("k".contains(letter)){
        score = 5;
    } else if("jx".contains(letter)){
        score = 8;
    } else if("qz".contains(letter)){
        score = 10;
    }

    return score;
}

public void linkDatabase(DatabaseHandler database){
    this.database = database;
}

public void setWordList(){
    this.words = this.database.getAllWords();
}
}

I just can't figure out why it won't work. 我只是不知道为什么它不起作用。

Are you calling setWordList() before calling other functions? 您是否在调用其他函数之前调用setWordList()? If not ArrayList words will be null 如果不是,则ArrayList的单词将为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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