2013-09-16 12 views
5

私はORMliteを使用していません。"android.app.Applicationをキャストすることができません"とアプリケーションクラッシュ "

@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> 
:だから、私はこの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; 
      } 
    } 
} 

、メインでを持って、私は問題を抱えているだけで、このラインを紹介します

メッセージエラーがある:

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 

できますが、これを解決するために私を助けてください?あなたは(すなわち、DtoFactory)カスタムApplicationを指定する必要が

+0

で問題は、メッセージが言うだけのようであるあなたがcom.exerciceにandroid.app.Applicationをキャストしようとしています.ormdatabase.DtoFactory、これは失敗します。 DtoFactoryはandroid.app.Applicationのサブクラスなので、DtoFactoryオブジェクトをandroid.app.Applicationにキャストできますが、その逆はできません。それについて考えると、欠けている情報はどこから来ますか? – bgse

答えて

29

の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> 
+0

私はそれを実行しました。私はmanifest.xmlをputedして動作しませんでした。 –

+0

あなたは 'Activity'としてインクルードしましたが、' Application'として指定する必要があります。更新された回答を確認してください。 – Rajesh

+1

あなたは私の人生を救った!ありがとうございます –

関連する問題