我是一个 Android 新手,我正在构建一个关于教程的 android 应用程序,我想在其中包含游戏功能,我已经有了游戏的源代码,并且我已经将所有类导入到我的项目中,但是当我尝试运行应用程序我收到一个错误,它说“android.app.application java.lang.ClassCastException 不能转换为 com.wglxy.example.dash1.ChuckApplication”,我认为这是因为我喜欢 2 个名为 HomeActivity 的活动.java (Main) 和 ChuckApplication.java (second) 具有不同的扩展 (DashboardActivity & Application) 我真的不知道如何解决这个问题,我尝试过
将游戏应用程序作为库(不工作)
结合活动(不工作)
修改 inten-filter > category (不起作用)
但我的项目没有任何作用
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
这是我的 ChuckApllicaiin:
/**
*
*/
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)
有人知道如何解决我的问题吗?