【问题标题】:Android Battery widget updates sometimesAndroid 电池小部件有时会更新
【发布时间】:2012-04-15 15:52:51
【问题描述】:

我正在尝试制作一个 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);
}

我的清单文件看起来像这样..

<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)

【问题讨论】:

  • 有没有人可以帮助我?
  • 你试过在调试器中运行它吗?在 onReceive/onUpdate 中放置一个断点,看看它是否会定期调用?如果没有更多信息,很难判断您的代码可能会出现什么问题。尝试打印到 logcat 以查看调用的内容和时间。也许你没有正确设置接收器,或者你的代码抛出了一个未捕获的异常。
  • 是的,有时会出现空指针异常。我在上面加了

标签: android widget power-management


【解决方案1】:

看看这个:

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

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

【讨论】:

  • 这是什么 if(level.intValue()
  • 我有 10 个 elseif。范围从 10 到 100。它会随机崩溃。
  • 您必须使用调试器并检查崩溃周围的变量。
  • 值为空。我认为这些值在更新之前就被处理掉了。从哪里开始使用服务来更新我的小部件?
猜你喜欢
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多