2013-07-10 50 views
0

我是一個Android新手,我正在構建一個關於教程的android應用程序,我想在其中包含遊戲功能,我已經有了源代碼代碼爲遊戲,我已經將所有類導入到我的項目中,但是當我嘗試運行該應用程序時出現錯誤,並且它說「android.app.application java.lang.ClassCastException不能轉換爲com.wglxy。 example.dash1.ChuckApplication「,我認爲這是因爲我fave 2活動稱爲HomeActivity.java(Main)和ChuckApplication.java(第二)與不同的擴展(DashboardActivity &應用程序)我真的不知道如何解決這個問題,已嘗試Android - java.lang.classcastexception android.app.application不能轉換

  1. Make the game applica重刑爲庫(不行)
  2. 合併活動(不行)
  3. 修改INTEN過濾器>類別(不工作)

    但並沒有什麼工作,我的項目

SplashActivity.java:

import java.io.IOException; 
    import java.util.List; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.database.SQLException; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    import com.tmm.android.chuck.db.DBHelper; 
    import com.tmm.android.chuck.quiz.Constants; 
    import com.tmm.android.chuck.quiz.GamePlay; 
    import com.tmm.android.chuck.quiz.Question; 

    public class SplashActivity extends Activity implements OnClickListener{ 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.welcome); 

      ////////////////////////////////////////////////////////////////////// 
      //////// GAME MENU ///////////////////////////////////////////////// 
      Button playBtn = (Button) findViewById(R.id.playBtn); 
      playBtn.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.exitBtn); 
      exitBtn.setOnClickListener(this); 
     } 


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

      switch (v.getId()){ 
      case R.id.playBtn : 
       //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); 

       //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.exitBtn : 
       finish(); 
       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, 20); 
      return numRounds; 
     } 

    } 

這是我HomeActivity.java

/* 
* Copyright (C) 2011 Wglxy.com 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
package com.wglxy.example.dash1; 

import android.os.Bundle; 

/** 
* This is a simple activity that demonstrates the dashboard user interface 
* pattern. 
* 
*/ 

public class HomeActivity extends DashboardActivity 
{ 

    /** 
    * onCreate - called when the activity is first created. Called when the 
    * activity is first created. This is where you should do all of your normal 
    * static set up: create views, bind data to lists, etc. This method also 
    * provides you with a Bundle containing the activity's previously frozen 
    * state, if there was one. 
    * 
    * Always followed by onStart(). 
    * 
    */ 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
    } 

    /** 
    * onDestroy The final call you receive before your activity is destroyed. 
    * This can happen either because the activity is finishing (someone called 
    * finish() on it, or because the system is temporarily destroying this 
    * instance of the activity to save space. You can distinguish between these 
    * two scenarios with the isFinishing() method. 
    * 
    */ 

    protected void onDestroy() 
    { 
     super.onDestroy(); 
    } 

    /** 
    * onPause Called when the system is about to start resuming a previous 
    * activity. This is typically used to commit unsaved changes to persistent 
    * data, stop animations and other things that may be consuming CPU, etc. 
    * Implementations of this method must be very quick because the next 
    * activity will not be resumed until this method returns. Followed by 
    * either onResume() if the activity returns back to the front, or onStop() 
    * if it becomes invisible to the user. 
    * 
    */ 

    protected void onPause() 
    { 
     super.onPause(); 
    } 

    /** 
    * onRestart Called after your activity has been stopped, prior to it being 
    * started again. Always followed by onStart(). 
    * 
    */ 

    protected void onRestart() 
    { 
     super.onRestart(); 
    } 

    /** 
    * onResume Called when the activity will start interacting with the user. 
    * At this point your activity is at the top of the activity stack, with 
    * user input going to it. Always followed by onPause(). 
    * 
    */ 

    protected void onResume() 
    { 
     super.onResume(); 
    } 

    /** 
    * onStart Called when the activity is becoming visible to the user. 
    * Followed by onResume() if the activity comes to the foreground, or 
    * onStop() if it becomes hidden. 
    * 
    */ 

    protected void onStart() 
    { 
     super.onStart(); 
    } 

    /** 
    * onStop Called when the activity is no longer visible to the user because 
    * another activity has been resumed and is covering this one. This may 
    * happen either because a new activity is being started, an existing one is 
    * being brought in front of this one, or this one is being destroyed. 
    * 
    * Followed by either onRestart() if this activity is coming back to 
    * interact with the user, or onDestroy() if this activity is going away. 
    */ 

    protected void onStop() 
    { 
     super.onStop(); 
    } 

    /** 
    */ 
    // Click Methods 

    /** 
    */ 
    // More Methods 

} // end class 

這是我ChuckApllicatiin:

/** 
* 
*/ 
package com.wglxy.example.dash1; 

import android.app.Application; 

/** 
* @author rob 
* 
*/ 
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; 
    } 
} 

這是我的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.wglxy.example.dash1" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <application 
     android:icon="@drawable/icon" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.D1" > 
     <activity 
      android:name=".HomeActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Tutorial" 
      android:label="@string/title_feature1" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Game" 
      android:label="@string/title_feature2" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Setting" 
      android:label="@string/title_feature3" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Qiblah" 
      android:label="@string/title_feature4" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Takbir" 
      android:label="@string/title_feature5" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Qiyam" 
      android:label="@string/title_feature6" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Ruku" 
      android:label="@string/title_feature7" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Sajdah" 
      android:label="@string/title_feature8" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".SitBetw" 
      android:label="@string/title_feature9" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Qunut" 
      android:label="@string/title_feature10" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Tahiyyatul" 
      android:label="@string/title_feature11" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Salam" 
      android:label="@string/title_feature12" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Subuh" 
      android:label="@string/title_feature13" 
      android:screenOrientation="portrait" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Dhuhur" 
      android:label="@string/title_feature14" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Asr" 
      android:label="@string/title_feature15" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Maghrib" 
      android:label="@string/title_feature17" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Isha" 
      android:label="@string/title_feature18" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Dhuha" 
      android:label="@string/title_feature19" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Witir" 
      android:label="@string/title_feature20" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Ied" 
      android:label="@string/title_feature21" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Tarawih" 
      android:label="@string/title_feature22" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Tahajud" 
      android:label="@string/title_feature23" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".BasicMain" 
      android:label="@string/title_feature23" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Fardh" 
      android:label="@string/title_feature23" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Sunnah" 
      android:label="@string/title_feature23" 
      android:theme="@style/Theme.D1" /> 
     <activity 
      android:name=".Dalil" 
      android:label="@string/title_feature23" 
      android:theme="@style/Theme.D1" /> 
     <!-- 
      <activity android:name=".AboutActivity" 
        android:theme="@style/Theme.D1" 
        android:label="@string/title_about" 
        /> 
      <activity android:name=".SearchActivity" 
        android:theme="@style/Theme.D1" 
        android:label="@string/title_search" 
        /> 
     --> 
     <activity 
      android:name=".SplashActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".QuestionActivity" /> 
     <activity android:name=".RulesActivity" /> 
     <activity android:name=".EndgameActivity" /> 
     <activity android:name=".SettingsActivity" /> 
     <activity android:name=".AnswersActivity" /> 
    </application> 
    <uses-sdk android:minSdkVersion="11" /> 
    <tool-api-level> 
     11 
    </tool-api-level> 
</manifest> 

,這是我的日誌:

07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): FATAL EXCEPTION: main 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): java.lang.ClassCastException: android.app.Application cannot be cast to com.wglxy.example.dash1.ChuckApplication 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at com.wglxy.example.dash1.SplashActivity.onClick(SplashActivity.java: 57) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at android.view.View.performClick(View.java: 4202) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at android.view.View$PerformClick.run(View.java: 17340) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at android.os.Handler.handleCallback(Handler.java: 725) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at android.os.Handler.dispatchMessage(Handler.java: 92) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at android.os.Looper.loop(Looper.java: 137) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at android.app.ActivityThread.main(ActivityThread.java: 5039) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at java.lang.reflect.Method.invokeNative(Native Method) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at java.lang.reflect.Method.invoke(Method.java: 511) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 793) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 560) 
07 - 10 11: 38: 01.946: E/AndroidRuntime(7548): at dalvik.system.NativeStart.main(Native Method) 

人知道如何解決我的問題?

+1

什麼是訂單SplashActivity.java:57 –

+0

請發表您的代碼的相關部分,其中有一個錯誤。 –

+0

@SriHarshaChilakapati對不起,我添加了SplashActivity.java @MichaelShrestha這是第57行 - 'code'GamePlay c = new GamePlay();'code' – dzulfikar

回答

1

您可以將子類對象轉換爲父類對象,但不能做相反的操作。您正嘗試將應用程序投射到ChuckApplication,這是不合法的。

相關問題