2013-04-12 42 views
0

基本上,当我单击小部件上的特定按钮时,我想要获取数据库中的下一条记录。该功能本身正常工作,因为我在应用程序中使用它。但在小部件中,它正在崩溃。我认为这是onreceive函数本身就是问题,但即使我只是设置onReceive来更新它崩溃的文本视图为nullpointerexception。Android小部件onReceive崩溃

public static String NEXT_RECORD =“next_record”;

这里是它被称为:

意图意图=新意图(上下文,MyWidgetProvider.class);
intent.setAction(NEXT_RECORD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.TextView02,pendingIntent);

和的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) 
+2

添加Logcat堆栈跟踪。 – tolgap

+0

我已经添加了。第100行是getNextRecord,但正如我所说的,我可以在其中添加任何内容,并且会崩溃。 – Paul

+1

你可以显示你的mDbHelper实例的创建位置吗? –

回答

0
Caused by: java.lang.NullPointerException 
    at com.example.app.hs.MyWidgetProvider.onReceive(MyWidgetProvider.java:100) 

如果newid = mDbHelper.getNextRecord(1, keyid);抛出此NPE然后mDbHelper为空。在尝试读取之前简单例示mDbHelper

@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); 
    } 

} 
+0

是的,这是问题。我没有意识到我需要再次实例化助手或远程视图。谢谢! – Paul