应用程序崩溃,并显示“ android.app.Application无法转换为” [英] Application crash with “android.app.Application cannot be cast to”
本文介绍了应用程序崩溃,并显示“ android.app.Application无法转换为”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在分配ORMlite。因此,我有这个DAO Factory类
I'm disovering ORMlite. So, I have this DAO Factory class
public class DtoFactory extends Application {
private SharedPreferences preferences;
private DatabaseHelper databaseHelper = null;
private Dao<ReleveEntity, Integer> releveDAO = null;
@Override
public void onCreate() {
super.onCreate();
preferences = PreferenceManager.getDefaultSharedPreferences(this);
databaseHelper = new DatabaseHelper(this);
}
public SharedPreferences getPreferences() {return preferences;}
public Dao<ReleveEntity, Integer> getReleveDao() throws SQLException, java.sql.SQLException {
if (releveDAO == null) {
releveDAO = databaseHelper.getDao(ReleveEntity.class);
}
return releveDAO;
}
@Override
public void onTerminate() {
super.onTerminate();
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
}
在主要方面,我会在出现问题的地方仅显示以下行:
And in the Main, I'll show just this lines where I have the problem :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dtoFactory = (DtoFactory) getApplication();
manifest.xml
And the manifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.exercice.ftouzi.ReleveActivity"
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="com.exercice.ormdatabase.DtoFactory" >
</activity>
</application>
消息错误为:
E/AndroidRuntime(19672): FATAL EXCEPTION: main
E/AndroidRuntime(19672): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.formation.adapter/com.exercice.ftouzi.ReleveActivity}:
java.lang.ClassCastException: android.app.Application cannot be cast to
com.exercice.ormdatabase.DtoFactory
您能帮我解决这个问题吗?
Can you please help me to resolve this ?
推荐答案
您需要在AndroidManifest.xml中指定自定义的 Application
(即 DtoFactory
)
You need to specify your custom Application
(i.e., DtoFactory
) in the AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.exercice.ormdatabase.DtoFactory">
<activity
android:name="com.exercice.ftouzi.ReleveActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这篇关于应用程序崩溃,并显示“ android.app.Application无法转换为”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文