gpt4 book ai didi

android - 在android小部件中从数据库获取数据

转载 作者:行者123 更新时间:2023-12-02 15:46:59 25 4
gpt4 key购买 nike

我已经为我的小部件创建了一个类,它应该从我的应用程序的数据库中获取数据,但我在获取数据时遇到问题...这是我的类

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;

}
}
}

在 list 中,一切都应该是这样,我已经创建了接收器和服务,在我添加带有光标的行之前,小部件显示在主屏幕上,但现在包含光标代码,它会导致应用程序崩溃并抛出空点IntentService 异常

最佳答案

我认为错误的是你应该编写这行代码

      mDbHelper=getDatabaseHelper(this);

之前

   Cursor ce= mDbHelper.fetchReminder(Integer.parseInt(dateForButton));

因为您已经定义了方法 getDatabaseHelper

关于android - 在android小部件中从数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com