0
嘗試在我已經構建的應用程序中加入測驗應用程序。我參加了測驗的教程。當我運行我的應用程序並單擊按鈕啓動測驗時,我得到一個java.lang.ClassCastException: android.app.Application cannot be cast to com.example.myfirstllapp.quiz.ChuckApplication
Android ClassCastException - 清單問題
我一直在研究,並且我的清單有問題。我似乎無法弄清楚。
下面是一個啓動測驗
package com.example.myfirstllapp;
//import android.R;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import com.example.myfirstllapp.quiz.ChuckApplication;
import com.example.myfirstllapp.quiz.Constants;
import com.example.myfirstllapp.quiz.DBHelper;
import com.example.myfirstllapp.quiz.GamePlay;
import com.example.myfirstllapp.quiz.Question;
import com.example.myfirstllapp.quiz.QuestionActivity;
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 android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Activia extends Activity implements OnClickListener {
private Button mButton;
private ImageView mImage;
private EditText mEditText;
private Button mChangeScreenButton;
private Button mWebButton;
private Button mPlayQuizButton;
private HashMap<String, Integer> mCharMap;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
initializeLayout();
//hashmap can be used instead of if thens
populateHashMap();
// Using HashMap might not need this yet
//mButton.setOnClickListener(this);
}
private void populateHashMap(){
mCharMap = new HashMap<String, Integer>();
mCharMap.put("bugsbunny", R.drawable.bugs);
mCharMap.put("clippy", R.drawable.clippy);
mCharMap.put("duder",R.drawable.duder);
mCharMap.put("cheesemouse",R.drawable.chuckecheese);
}
private void initializeLayout() {
setContentView(R.layout.ugly_layout);
mPlayQuizButton = (Button)findViewById(R.id.play_the_quiz);
mPlayQuizButton.setOnClickListener(this);
mButton = (Button)findViewById(R.id.button);
mImage = (ImageView)findViewById(R.id.image_view);
mEditText = (EditText)findViewById(R.id.edit_text);
mChangeScreenButton = (Button)findViewById(R.id.switch_screen_button);
mWebButton = (Button)findViewById(R.id.web_button);
//using hashmap call down here, I dont know why tho
mButton.setOnClickListener(this);
mChangeScreenButton.setOnClickListener(this);
mWebButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button :
String enteredValue = mEditText.getText().toString();
enteredValue = enteredValue.toLowerCase().replace(" ", "");
if(enteredValue.length() == 0){
Toast.makeText(this," You need to enter something",Toast.LENGTH_SHORT).show();
}
Integer drawableId = mCharMap.get(enteredValue);
if(drawableId != null) {
mImage.setImageDrawable(getResources().getDrawable(drawableId));
}
else{
Toast.makeText(this,"Sorry The character you entered is not supported.",Toast.LENGTH_SHORT).show();
}
mEditText.setText("");
break;
case R.id.switch_screen_button :
Toast.makeText(this,"it clicks",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, CoolerActivity.class);
startActivity(intent);
break;
case R.id.web_button :
Toast.makeText(this,"it clicks",Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(this, WebActivity.class);
startActivity(intent2);
break;
case R.id.play_the_quiz :
Toast.makeText(this,"It Clicks2",Toast.LENGTH_SHORT).show();
//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.. //
Intent i = new Intent(this, QuestionActivity.class);
startActivityForResult(i, Constants.PLAYBUTTON);
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;
}
,這裏的活動代碼是清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstllapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true" >
<activity
android:name="com.example.myfirstllapp.Activia"
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=".CoolerActivity" />
<activity android:name=".WebActivity" />
<activity android:name="com.example.myfirstllapp.quiz.QuestionActivity" />
<activity android:name="com.example.myfirstllapp.quiz.RulesActivity" />
<activity android:name="com.example.myfirstllapp.quiz.EndgameActivity" />
<activity android:name="com.example.myfirstllapp.quiz.SettingsActivity" />
<activity android:name="com.example.myfirstllapp.quiz.AnswersActivity" />
<activity android:name="com.example.myfirstllapp.quiz.ChuckApplication" />
</application>
<application
android:name=".ChuckApplication" >
</application>
</manifest>
首先,您的清單中只能有一個''標記。刪除第二個。另外,'com.example.myfirstllapp.quiz.ChuckApplication'是一個Activity嗎?如果不從清單中刪除這行' ''。 –
Akash
2013-02-22 06:10:17