AppWidget在onDisabled中崩溃

时间:2012-10-31 18:11:11

标签: android widget android-appwidget

我制作小工具,将由Intent.ACTION_TIME_TICK更新。

所以我注册了收件人(我的AppWidgetProvider):

private IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);

@Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        context.getApplicationContext().registerReceiver(this, intentFilter);   
    }

一切正常。但当我尝试取消注册接收器时,应用程序崩溃了:

@Override
public void onDisabled(Context context) {   
    context.getApplicationContext().unregisterReceiver(this);  
    super.onDisabled(context);
}

它抛出

java.lang.IllegalArgumentException: Receiver not registered:
com.holoware.holoclock.HoloClockWidgetProvider@41a3a3e8

日志:

D/AndroidRuntime( 3937): Shutting down VM
W/dalvikvm( 3937): threadid=1: thread exiting with uncaught exception (group=0x4
19e8300)
E/AndroidRuntime( 3937): FATAL EXCEPTION: main
E/AndroidRuntime( 3937): java.lang.RuntimeException: Unable to start receiver co
m.holoware.holoclock.HoloClockWidgetProvider: java.lang.IllegalArgumentException
: Receiver not registered: com.holoware.holoclock.HoloClockWidgetProvider@41d616
20
E/AndroidRuntime( 3937):        at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:2362)
E/AndroidRuntime( 3937):        at android.app.ActivityThread.access$1500(Activi
tyThread.java:142)
E/AndroidRuntime( 3937):        at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:1284)
E/AndroidRuntime( 3937):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 3937):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3937):        at android.app.ActivityThread.main(ActivityThrea
d.java:4931)
E/AndroidRuntime( 3937):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 3937):        at java.lang.reflect.Method.invoke(Method.java:5
11)
E/AndroidRuntime( 3937):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 3937):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:558)
E/AndroidRuntime( 3937):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime( 3937): Caused by: java.lang.IllegalArgumentException: Receiver
 not registered: com.holoware.holoclock.HoloClockWidgetProvider@41d61620
E/AndroidRuntime( 3937):        at android.app.LoadedApk.forgetReceiverDispatche
r(LoadedApk.java:654)
E/AndroidRuntime( 3937):        at android.app.ContextImpl.unregisterReceiver(Co
ntextImpl.java:1166)
E/AndroidRuntime( 3937):        at android.content.ContextWrapper.unregisterRece
iver(ContextWrapper.java:378)
E/AndroidRuntime( 3937):        at com.holoware.holoclock.HoloClockWidgetProvide
r.onDisabled(HoloClockWidgetProvider.java:53)
E/AndroidRuntime( 3937):        at android.appwidget.AppWidgetProvider.onReceive
(AppWidgetProvider.java:91)
E/AndroidRuntime( 3937):        at com.holoware.holoclock.HoloClockWidgetProvide
r.onReceive(HoloClockWidgetProvider.java:63)
E/AndroidRuntime( 3937):        at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:2355)
E/AndroidRuntime( 3937):        ... 10 more

2 个答案:

答案 0 :(得分:0)

确保您的onDisabled()没有被调用两次,同时检查您是否也在某种其他方法中取消注册接收器...

尝试将未注册的代码放在try catch中,如下面的代码..

@Override
    public void onDisabled(Context context) {       
        try{
                context.getApplicationContext().unregisterReceiver(this);
         } catch(Exception e){}
        super.onDisabled(context);
    }

答案 1 :(得分:0)

问题是,只有在添加第一个窗口小部件时才会调用onEnabled()。如果已添加窗口小部件并启动应用程序,则不会调用onEnabled()。这就是为什么接收器没有注册,你得到一个异常取消注册它。我建议你这样创建Intent

    Intent intent = new Intent(context, YourAppWidgetProvider.class);
    intent.setAction(action);