Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application crash with “android.app.Application cannot be cast to”

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();

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>

The message error is :

 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 ?

like image 818
Farouk Touzi Avatar asked Sep 16 '13 14:09

Farouk Touzi


1 Answers

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>
like image 79
Rajesh Avatar answered Nov 08 '22 14:11

Rajesh