0

其实我真的不知道我的错误是什么。我运行了相同的代码,但它运行完美。但我修改后,它有错误。LogCat 具有与正常相同的日志。这是调试中的内容:

Thread [<1> main] (Suspended (exception ClassCastException))
<VM does not provide monitor information>   
SplashActivity.onClick(View) line: 65   
Button(View).performClick() line: 2485
View$PerformClick.run() line: 9080
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839  
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]

下面是我的代码:我突出显示了 Eclipse 突出显示的错误。

public class SplashActivity extends Activity implements OnClickListener{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.welcome);

    Button play1Btn = (Button) findViewById(R.id.mainplayBtn);
    play1Btn.setOnClickListener(this);
    Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
    settingsBtn.setOnClickListener(this);
    Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
    rulesBtn.setOnClickListener(this);
    Button exitBtn = (Button) findViewById(R.id.gmenuBtn);
    exitBtn.setOnClickListener(this);
}


/**
 * Listener for game menu
 */
@Override
public void onClick(View v) {
    Intent i;

    switch (v.getId()){
    case R.id.mainplayBtn :
        //once logged in, load the main page
        //Log.d("LOGIN", "User has started the game");

        //Get Question set //
        List<Question> questions = getQuestionSetFromDb();

        //Initialise Game with retrieved question set ///
        GamePlay c = new GamePlay();
        c.setQuestions(questions);
        c.setNumRounds(getNumQuestions());
        **((ChuckApplication)getApplication()).setCurrentGame(c);** ***<--Eclipse highlight this as error.*** 

        //Start Game Now.. //
        i = new Intent(this, QuestionActivity.class);
        startActivityForResult(i, Constants.PLAYBUTTON);
        break;

    case R.id.rulesBtn :
        i = new Intent(this, RulesActivity.class);
        startActivityForResult(i, Constants.RULESBUTTON);
        break;

    case R.id.settingsBtn :
        i = new Intent(this, SettingsActivity.class);
        startActivityForResult(i, Constants.SETTINGSBUTTON);
        break;

    case R.id.gmenuBtn :
        i = new Intent(this, Main.class);
        startActivityForResult(i, Constants.GAMEMENUBUTTON);
        break;
    }

}


/**
 * Method that retrieves a random set of questions from
 * the database for the given difficulty
 * @return
 * @throws Error
 */
private List<Question> getQuestionSetFromDb() throws Error {
    int diff = getDifficultySettings();
    int numQuestions = getNumQuestions();
    DBHelper myDbHelper = new DBHelper(this);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
    List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
    myDbHelper.close();
    return questions;
}


/**
 * Method to return the difficulty settings
 * @return
 */
private int getDifficultySettings() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
    return diff;
}

/**
 * Method to return the number of questions for the game
 * @return
 */
private int getNumQuestions() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int numRounds = settings.getInt(Constants.NUM_ROUNDS, 10);
    return numRounds;
}

下面是 ChuckApplication.java

public class ChuckApplication extends Application{
private GamePlay currentGame;

/**
 * @param currentGame the currentGame to set
 */
public void setCurrentGame(GamePlay currentGame) {
    this.currentGame = currentGame;
}

/**
 * @return the currentGame
 */
public GamePlay getCurrentGame() {
    return currentGame;
}
4

1 回答 1

0

您可能忘记将应用程序类添加到清单中。

<application android:name=".ChuckApplication" .../>

应该是诀窍。

于 2013-04-29T14:31:01.800 回答