Android的 - java.lang.classcastexception android.app.application无法施放lang、java、Android、classcastexcep

由网友(一身王傲气)分享简介:我是一个Android的新手,我要建约教程一个Android应用程序,我想包括在它的游戏功能,我已经有源$ C ​​$下的比赛,我已经所有的类导入到我的项目,但是当我尝试运行该应用程序我得到一个错误,它说:android.app.application java.lang.ClassCastException不能转换为...

我是一个Android的新手,我要建约教程一个Android应用程序,我想包括在它的游戏功能,我已经有源$ C ​​$下的比赛,我已经所有的类导入到我的项目,但是当我尝试运行该应用程序我得到一个错误,它说:android.app.application java.lang.ClassCastException不能转换为com.wglxy.example.dash1.ChuckApplication,我认为这是因为我最喜欢的2活性称为HomeActivity.java(主)和ChuckApplication.java(二)具有不同的扩展(DashboardActivity&安培;应用)我真的不知道该如何解决这个问题,我已经尝试

I'm an Android newbie, and I'm building an android app about tutorial and I want to include game features in it, I already have the source code for the game and i already import all the classes to my project, however when i try to run the app i got an error and it said "android.app.application java.lang.ClassCastException can not be cast to com.wglxy.example.dash1.ChuckApplication", i assume that is because i fave 2 activity called HomeActivity.java (Main) and ChuckApplication.java (second) with different extend (DashboardActivity & Application) i really do not know how to solve this, i've try

请在游戏应用程序库(没有工作) 联合活动(无效)

修改INTEN过滤器>类别(不工作) Make the game application as library(not work) Combine the activities (not work)

modify inten-filter > category (not work)

但并没有什么工作,我的项目

but there is nothing work with my project

SplashActivity.java:

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

this is my 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:

this is my 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

this is my 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>

这是我的日志:

and this is my LOG:

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)

人知道如何解决我的问题?

anyone know how to fix my problem?

推荐答案

您可以投子类对象的父类对象,但你不能做反向。您正在尝试施放应用ChuckApplication,这是不合法的。

You can cast child class object to parent class object but you cannot do the reverse. You are trying to cast Application to ChuckApplication, which is not legal.

阅读全文

相关推荐

最新文章