gpt4 book ai didi

android - 应用程序崩溃 “android.app.Application cannot be cast to”

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:18 27 4
gpt4 key购买 nike

我发现了 ORMlite。所以,我有这个 DAO 工厂类

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;
}
}
}

在主要部分,我将只显示我遇到问题的这几行:

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dtoFactory = (DtoFactory) getApplication();

和 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

你能帮我解决这个问题吗?

最佳答案

您需要在 AndroidManifest.xml 中指定您的自定义 Application(即 DtoFactory)

   <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 - 应用程序崩溃 “android.app.Application cannot be cast to”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com