Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

exception - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...