Android-java.lang.classcastexception Android.app.application无法强制转换
我是一个Android新手,我正在构建一个关于教程的Android应用程序,我想在其中包含游戏功能,我已经有了游戏的源代码,我已经将所有类导入到我的项目中,但是当我尝试运行应用程序时,我遇到了一个错误,它说“android.app.application java.lang.ClassCastException无法转换为com.wglxy.example.dash1.ChuckApplication”,我假设这是因为我喜欢两个名为HomeActivity.java(Main)和ChuckApplication.java(second)的活动,它们具有不同的扩展(DashboardActivity&application)我真的不知道如何解决这个问题,我已经尝试过了Android-java.lang.classcastexception Android.app.application无法强制转换,java,android,android-activity,Java,Android,Android Activity,我是一个Android新手,我正在构建一个关于教程的Android应用程序,我想在其中包含游戏功能,我已经有了游戏的源代码,我已经将所有类导入到我的项目中,但是当我尝试运行应用程序时,我遇到了一个错误,它说“android.app.application java.lang.ClassCastException无法转换为com.wglxy.example.dash1.ChuckApplication”,我假设这是因为我喜欢两个名为HomeActivity.java(Main)和ChuckAppl
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;
}
}
这是我的笑话:
/**
*
*/
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
11
这是我的日志:
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)
有人知道如何解决我的问题吗?您可以将子类对象强制转换为父类对象,但不能执行相反的操作。您试图将应用程序强制转换为父类对象,这是不合法的。问:您在清单中的何处指定了应用程序类?例如:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/Theme.D1"
android:name="com.wglxy.example.dash1.ChuckApplication" >
...
SplashActivity行中有什么内容。java:57请在出现错误的地方发布代码的相关部分。@SriHarshaChilakapati抱歉,我已经添加了SplashActivity.java@MichaelShrestha这是第57行-code
GamePlay c=new GamePlay();code
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/Theme.D1"
android:name="com.wglxy.example.dash1.ChuckApplication" >
...