Android Battery小部件有时会更新

时间:2012-04-15 15:52:51

标签: android widget power-management

我正在尝试制作Android电池小部件。问题是有时它会更新,但有时却不会。

我必须重新启动手机才能再次使用 - 或者将其从屏幕上删除并再次在屏幕上创建。

我的问题是什么?

这是我的代码..这是我唯一的课程。

public class BatteryWidgetProvider extends AppWidgetProvider
{
    private RemoteViews views;
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) 
    {

    context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    ComponentName name = new ComponentName(context, BatteryWidgetProvider.class);
    appWidgetManager.updateAppWidget(name, this.views);
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) 
{
    String action = intent.getAction();
    if(action.equals(intent.ACTION_BATTERY_CHANGED))
    {
        Integer level = intent.getIntExtra("level", -1);

        this.views.setTextViewText(R.id.batter_level, level.toString() + "%");
        ComponentName name = new ComponentName(context, BatteryWidgetProvider.class);
        AppWidgetManager.getInstance(context).updateAppWidget(name, this.views);
    }
    super.onReceive(context, intent);
}

My Manifest文件看起来像这样..

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver  android:name=".BatteryWidgetProvider">
        <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_info"/>   
    </receiver>
</application>

这是我的信息文件......

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minHeight="72dp"
    android:minWidth="144dp"
    android:updatePeriodMillis="0" >
</appwidget-provider>

错误....

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.batterywidget.BatteryWidgetProvider@4a38e3f0
at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:906)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.batterywidget.BatteryWidgetProvider.onReceive(BatteryWidgetProvider.java:72)
at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:893)

1 个答案:

答案 0 :(得分:0)

看着这个:

Caused by: java.lang.NullPointerException
at com.batterywidget.BatteryWidgetProvider.onReceive(BatteryWidgetProvider.java:72)

这意味着您在第72行的BatteryWidgetProvider中有一个例外。您可以查找该行并查看您在那里引用哪些可能为null的对象吗?