启动活动时崩溃

时间:2016-04-30 03:10:44

标签: java android android-activity serialization android-asynctask

我正在尝试从此类中的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);
    }
}

}

但是每当它试图启动活动时,它突然崩溃而没有错误,但我确实设法在某处发现一个错误,表示Dictionary不可序列化,但它是:

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();
}
}

我无法弄清楚为什么它不起作用。

1 个答案:

答案 0 :(得分:0)

你在调用其他函数之前调用setWordList()吗?如果不是ArrayList单词将为null