0
我想開始從onPostExecute()方法的活動在此上課的時候崩潰:開展活動
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);
}
}
}
但是每當它嘗試啓動活動,它沒有錯誤突然死機,但我沒有設法找到一個錯誤的地方,說字典是不是序列化的,但它是:
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();
}
}
我只是想不通,爲什麼它不會工作。
我會想象它是'Dictionary'類中的'DatabaseHandler'這就是問題所在。 –
這很奇怪,因爲它以前從來都不是問題......我會看看。 –