0

我想从一个intentservice使用Ormlite进行数据库处理。我已阅读有关使用应用程序作为子类。我曾尝试以下Ormlite内部Intentservice - ClassCastException

的应用类:

public class MyApplication extends Application { 
    private volatile DatabaseHelper databaseHelper = null; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 


    public DatabaseHelper getHelper() { 
     if (databaseHelper == null) { 
      databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class); 
     } 
     return databaseHelper; 
    } 
} 

在我所限定的意图服务:

private DatabaseHelper databaseHelper; 

而在我曾尝试intentservice的onHandleIntent()方法:

databaseHelper = ((MyApplication) getApplicationContext()).getHelper(); 

但是我得到ClassCastException,从应用程序到MyApplicati在

有人可以向我解释我做错了什么吗?

+0

哪里是ormlite代码???我不认为你需要这样做,如果你正在使用ormlite –

+0

@PankajKumar是在活动中使用Ormlite,但是活动需要扩展OrmLiteBaseActivity 。需要此解决方案才能使其从intentservice开始工作。 – user1851212

回答

0

发现问题。我在清单中声明了与我的主应用程序不同的应用程序。 我需要的是修改AndroidManifest.xml中的主应用程序。我指着主应用程序,我创建 自定义应用程序(为MyApplication)补充一点:

<Application android:name=".MyApplication">