2013-04-29 104 views
0

其實我真的不知道我的錯誤是什麼。我運行了相同的代碼,但運行完美。但是,我修改後,它有錯誤。 LogCat具有與正常相同的日誌。這裏有一些在調試:onClick意外錯誤getApplication

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; 
} 
+0

你可以添加清單嗎?你修改了什麼? – 2013-04-29 14:25:01

+0

第65行在哪裏,它的哪一行代碼完全錯誤?你有沒有在清單中聲明所有的活動? – JPM 2013-04-29 14:25:40

+0

第65行是:(((ChuckApplication)getApplication())。setCurrentGame(c); – bbvip 2013-04-29 15:06:48

回答

0

你可能忘了您的應用程序類添加到清單。

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

應的伎倆。