0

I am trying to make an Android battery widget. The problem is that sometimes it updates, but sometimes it doesn't.

I would have to restart my phone for it to work again – or delete it from the screen and create it on the screen again.

What could my problem be?

Here is my code.. This is my only class.

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 file looks like this..

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

Here is my info file...

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

The error....

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)
jimmym715
  • 1,512
  • 1
  • 16
  • 25
Mich
  • 105
  • 1
  • 2
  • 9
  • Is there anyone that can help me? – Mich Apr 16 '12 at 04:29
  • Have you tried running this in a debugger? Place a breakpoint in onReceive/onUpdate and see if it gets called on a regular interval? It's difficult to tell what could potentially go wrong with your code without more information. Try some prints to logcat to see what's getting called and when. Maybe you haven't set up the receiver correctly, or maybe your code is throwing an uncaught exception. – Tim Apr 16 '12 at 05:07
  • Yes sometimes there is a nullpointer exception. I added it above – Mich Apr 19 '12 at 22:36

1 Answers1

0

Looking at this:

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

This means that you have an exception in BatteryWidgetProvider, line 72. Can you lookup that line and see what objects you're referencing there that could be null?

Tim
  • 35,413
  • 11
  • 95
  • 121
  • This is what is there else if(level.intValue()< 10) { this.views.setTextViewText(R.id.batter_level, level.toString() + "%"); } – Mich Apr 19 '12 at 23:40
  • I have 10 af those elseif. ranging from 10-100. It crashes on anyone randomly. – Mich Apr 19 '12 at 23:50
  • You'll have to use your debugger and inspect the variables around the crash. – Tim Apr 20 '12 at 00:48
  • The values are empty. I think the values are being disposed of before they get to the update. Where to start on using a service to update my widget? – Mich Apr 20 '12 at 01:14