Android widget onReceive crashing
Basically when I click a particular button on the widget I want to fetch the next record in the database. The function itself works fine as I use it in the app. But in the widget it's crashing. I'm thinking it's the onReceive function itself that is the problem though as even I just set the onReceive to update a textview it crashes as nullpointerexception.
public static String NEXT_RECORD = "next_record";
Here is where it gets called:
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(NEXT_RECORD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.TextView02, pendingIntent);
and onreceive
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(NEXT_RECORD.equals(intent.getAction())){
newid = mDbHelper.getNextRecord(1, keyid);
}
}
04-12 14:47:57.591: E/AndroidRuntime(30633): FATAL EXCEPTION: main
04-12 14:47:57.591: E/AndroidRuntime(30633): java.lang.RuntimeException: Unable to start receiver com.example.app.hs.MyWidgetProvider: java.lang.NullPointerException
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2362)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread.access$1500(ActivityThread.java:142)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.os.Looper.loop(Looper.java:137)
04-12 14:47:57.591: E/AndroidRuntime(30633): at android.app.ActivityThread.main(ActivityThread.java:4931)
04-12 14:47:57.591: E/AndroidRuntime(30633): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 14:47:57.591: E/AndroidRuntime(30633): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 14:47:57.591: E/AndroidRuntime(30633): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-12 14:47:57.591: E/AndroidRuntime(30633): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-12 14:47:57.591: E/AndroidRuntime(30633): at dalvik.system.NativeStart.main(Native Method)
04-12 14:47:57.591: E/AndroidRuntime(30633): Caused by: java.lang.NullPointerException
04-12 14:47:57.591: E/AndroidRuntime(30633): at com.example.app.hs.MyWidgetProvider.onReceive(MyWidgetProvider.java:100)
Caused by: java.lang.NullPointerException
at com.example.app.hs.MyWidgetProvider.onReceive(MyWidgetProvider.java:100)
If newid = mDbHelper.getNextRecord(1, keyid);
throws this NPE then mDbHelper
is null. Simply instantiate mDbHelper
before trying to read from it:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (NEXT_RECORD.equals(intent.getAction())) {
mDbHelper = new DbHelper(context); // I guessed at the constructor's parameters
newid = mDbHelper.getNextRecord(1, keyid);
}
}
链接地址: http://www.djcxy.com/p/50648.html
上一篇: 如何解决服务实例问题?