我为我的小部件创建了一个类,它应该从我的应用程序的数据库中获取数据,但是我在获取数据方面遇到了问题......这是我的班级
public class AppWidget extends AppWidgetProvider {
private static DBAdapter mDbHelper;
@Override
public void onReceive(Context ctxt, Intent intent)
{
if(intent.getAction()==null)
{
ctxt.startService(new Intent (ctxt,ToggleService.class));
}
else
{
super.onReceive(ctxt,intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
context.startService(new Intent(context,ToggleService.class));
}
public static void updateWidgetContent(Context context,
AppWidgetManager appWidgetManager, int[] appWidgetIds) {
}
private static DBAdapter getDatabaseHelper(Context context) {
DBAdapter dbh = null;
if (dbh == null) {
dbh = new DBAdapter(context);
dbh.open();
}
return dbh;
}
public static class ToggleService extends IntentService
{
public ToggleService()
{
super("AppWidget$ToggleService");
}
@Override
protected void onHandleIntent(Intent intent)
{
ComponentName me= new ComponentName(this,AppWidget.class);
AppWidgetManager mgr= AppWidgetManager.getInstance(this);
mgr.updateAppWidget(me,buildUpdate(this));
}
private RemoteViews buildUpdate(Context context)
{
RemoteViews updateViews= new RemoteViews(context.getPackageName(),R.layout.widget);
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
String dateForButton= dateFormat.format(c.getTime());
Cursor ce= mDbHelper.fetchReminder(Integer.parseInt(dateForButton));
ce.moveToFirst();
//startManagingCursor(ce);
updateViews.setTextViewText(R.id.dd,ce.getString(ce.getColumnIndexOrThrow(DBAdapter.KEY_de)));
updateViews.setTextViewText(R.id.tt,"test2");
updateViews.setTextViewText(R.id.ss,"test1");
Intent i= new Intent(this, AppWidget.class);
PendingIntent pi= PendingIntent.getBroadcast(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.kr, pi);
return updateViews;
}
}
}
在清单中一切都是应有的,我已经创建了接收器和服务,在我添加光标线之前,小部件在主屏幕上显示但是现在包含光标代码它会崩溃应用程序抛出零点IntentService上的异常
答案 0 :(得分:0)
问题应该是你在实例化之前使用mDbHelper
。
我建议您首先初始化您的适配器,如下所示:
private static DBAdapter getDatabaseHelper(Context context) {
if (mDbHelper == null) {
mDbHelper = new DBAdapter(context);
mDbHelper.open();
}
return mDbHelper;
}
如果这不能解决您的问题,请发布您的LogCat
答案 1 :(得分:0)
我认为错误是你应该编写这行代码
mDbHelper=getDatabaseHelper(this);
之前
Cursor ce= mDbHelper.fetchReminder(Integer.parseInt(dateForButton));
因为您已经定义了方法getDatabaseHelper