我正在使用下面的示例观察表代码 https://github.com/googlecodelabs/watchface
我正在尝试将电池级别的信息添加到手表,但我会收到此错误。
12-20 11:39:15.042 2914-2914/com.example.android.wearable.watchface E/Androidruntime:致命例外:主要 过程:com.example.android.wearable.watchface,pid:2914 java.lang.runtimeException:无法实例化服务 com.example.android.wearable.watchface.watchface.digitalwatchfaceservice: java.lang.nullpointerexception:尝试调用虚拟方法 'android.content.intent android.content.Context.RegisterReceiver(android.content.Broadcastreceiver, android.content.intentfilter)'on null对象参考 android.app.activityThread.HandleCreateService(activityThread.java:3389) at android.app.activitythread.-wrap4(未知来源:0)
在 Android.App.ActivityThread $ H.HANDLEMESSAGE(activityThread.java:1683) 在Android.os.handler.dispatchMessage(Handler.java:105)
at android.os.looper.loop(looper.java:164)
at android.app.activitythread.main(activityThread.java:6541)
在java.lang.reflect.method.invoke(本机方法)
在 com.android.internal.os.zygote $ methodandargscaller.run(zygote.java:240) 在com.android.internal.os.os.zygoteinit.main(zygoteinit.java:767)
引起:java.lang.nullpointerexception:尝试调用虚拟 方法'android.content.intent android.content.Context.RegisterReceiver(android.content.Broadcastreceiver, android.content.intentfilter)'在空对象引用
上 在 android.content.contextwrapper.registerreceiver(contextWrapper.java:603) 在 com.example.android.wearable.watchface.watchface.digitalwatchfaceservice.(digitalWatchFaceservice.java:81) 在java.lang.class.newinstance(本机方法)
在 android.app.activityThread.HandleCreateService(activityThread.java:3386) at android.app.activitythread.-wrap4(未知来源:0)
在 Android.App.ActivityThread $ H.HANDLEMESSAGE(activityThread.java:1683) 在android.os.handler.dispatchmessage(handler.java:105)at android.os.looper.loop(looper.java:164)
at android.app.activitythread.main(activityThread.java:6541)
在java.lang.reflect.method.invoke(本机方法)
在 com.android.internal.os.zygote $ methodandargscaller.run(zygote.java:240) 在com.android.internal.os.os.zygoteinit.main(zygoteinit.java:767)12-20 11:39:15.082 2914-2914/com.example.android.wearable.watchface I/过程:发送信号。PID:2914 SIG:9
这是导致错误
的" DigitalWatchFaceservice.java"中的代码。// Battery code
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
这使用此操作:
公共字符串getBatteryLevel(){ Intent Battureintent = registerReceiver(null,new IntentFilter(Intent.Action_Battery_changed)); int Level = PowdateIntent.GetIntExtra(BatteryManager.extra_level,-1); int scale = downaterIntent.getIntextra(batterymanager.extra_scale,-1);
// Error checking that probably isn't needed but I added just in case.
if(level == -1 || scale == -1) {
return "error";
}
float batteryLevelWithDecimals = ((float)level / (float)scale) * 100.0f;
String battery = String.format("%.0f", batteryLevelWithDecimals);
return battery ;
}
然后,您可以像这样调用电池级字符串:
canvas.drawText(
String.valueOf(getBatteryLevel()),
mXOffset, mYOffset + mLineHeight * 3, mDatePaint);