0

尝试将测验应用程序合并到我已经构建的应用程序中。我遵循了测验的教程。当我运行我的应用程序并单击按钮启动测验时,我得到一个java.lang.ClassCastException: android.app.Application cannot be cast to com.example.myfirstllapp.quiz.ChuckApplication

我一直在研究,我的清单有问题。我似乎无法弄清楚。

这是启动测验的活动的代码

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>
4

1 回答 1

0

看看http://developer.android.com/guide/topics/manifest/application-element.html中应用程序元素的定义。活动应该是应用程序的子项。

于 2013-02-22T06:39:50.190 回答