我正在研究一个琐事风格的应用程序,我正在片段中使用它。如果我使用Activities而不是Fragments,代码可以工作:
((ChuckApplication)getApplication()).setCurrentGame(c);
但是一旦我将它投射到一个活动,它就会继续出错。代码是:
((ChuckApplication)getActivity().getApplication()).setCurrentGame(c);
这个片段的整个代码是:
public class Activity_Home_Language extends Fragment implements OnClickListener{
Intent intent;
ImageButton btnToggle;
Button btnExam,btnReview;
TextView txtTitle;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_home_language, container, false);
btnToggle = (ImageButton) rootView.findViewById(R.id.btnToggle);
btnExam = (Button) rootView.findViewById(R.id.btnExam);
btnReview = (Button) rootView.findViewById(R.id.btnReview);
txtTitle = (TextView) rootView.findViewById(R.id.txtTitle);
btnToggle.setOnClickListener(this);
btnExam.setOnClickListener(this);
btnReview.setOnClickListener(this);
//FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
//transaction.add(R.id.frameContent, new Activity_Home());
//transaction.add(R.id.framePager2, new ViewPagerMunicipalities());
//transaction.add(R.id.frameDestPager, new ViewPagerDes());
//transaction.commit();
//for fading animation
return rootView;
}
@Override
public void onClick(View v) {
if(v==btnToggle){
Activity_Main.mSlideHolder.open();
}
else if(v==btnExam){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
transaction.replace(R.id.frameContent, new Activity_Question_Exam_Home()).commit();
}
else if(v==btnReview){
//enable this to move to move to a Activity or fragment activity
//intent = new Intent(rootView.getContext(), Activity_About.class);
//startActivityForResult(intent,0);
//Get Question set //
List<Question> questions = getQuestionSetFromDb();
//Initialise Game with retrieved question set ///
GamePlay c = new GamePlay();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((ChuckApplication) getActivity().getApplication()).setCurrentGame(c);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frameContent, new Activity_Home_Language_Review()).commit();
//transaction.addToBackStack(null);
//transaction.commit();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.addToBackStack(null);
//transaction.replace(R.id.frameContent, new Activity_Question_Review_Home()).commit();
}
else {
}
}
/**
* 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(getActivity());
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);
//List<Question> questions = myDbHelper.getQuestionSet(diff, 2);
myDbHelper.close();
return questions;
}
/**
* Method to return the difficulty settings
* @return
*/
private int getDifficultySettings() {
SharedPreferences settings = getActivity().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 = getActivity().getSharedPreferences(Constants.SETTINGS, 0);
int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
return numRounds;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
请帮忙。我被困在那条线上。
以下是申请表:
package com.example.civilserviceexamreviewer;
import android.app.Application;
import com.example.civilserviceexamreviewer.quiz.GamePlay;
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;
}
}
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.civilserviceexamreviewer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash1"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Splash2"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Activity_Main"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Question_Review_Home"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_About"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.civilserviceexamreviewer.Activity_Choices"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
</application>
</manifest>
答案 0 :(得分:0)
从Fragment中访问应用程序(或其子类)的方式是正确的。由于您收到的错误是ClassCastException
,我认为您的问题是您尚未在Application
的{{1}}标记中声明自定义AndroidManifest.xml
子类。因此,调用application
实际上将返回类型为.getApplication()
的对象,该对象无法对您的自定义Application类进行类型转换。也许你在原来基于android.app.Application
的项目中这样做了,但在创建基于Activity
的项目时却忘了这样做了?
如何在清单文件中声明Fragments
子类的示例:
Application
因此,您在问题中发布的完整清单应该是:
<application
android:name="com.example.civilserviceexamreviewer.ChuckApplication"
android:icon="..."
android:label="...">
</application>