Domanda

i've created a class for my widget that should get data from the database of my app, but i have issues getting the data...here is my class

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;

    }
}
 }

in the manifest everything is as it should, i have created the receiver and a service and before i added the line with the cursor the widget showed on the home screen but now with the cursor code included it crashes the app throwing null point exception on IntentService

È stato utile?

Soluzione

what i see as error is that you should write this line of code

      mDbHelper=getDatabaseHelper(this);

before

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

because you have already defined the method getDatabaseHelper

Altri suggerimenti

The problem should be that u are using mDbHelper before instantiation.

I suggest you to initialize first your Adapter as follow:

private static DBAdapter getDatabaseHelper(Context context) {

    if (mDbHelper == null) {
        mDbHelper = new DBAdapter(context);
        mDbHelper.open();
    }
    return mDbHelper;
}

If this won't resolve your problem, please post your LogCat

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top