我解除了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
你能帮我解决这个问题吗?
答案 0 :(得分:31)
您需要在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>