我正在使用下面的Sample Watchface代码 https://github.com/googlecodelabs/watchface
我正在尝试将电池电量信息添加到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)'在空对象引用上 android.app.ActivityThread.handleCreateService(ActivityThread.java:3389) 在android.app.ActivityThread.-wrap4(未知来源:0)
在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1683) 在android.os.Handler.dispatchMessage(Handler.java:105)
在android.os.Looper.loop(Looper.java:164)
在android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.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) at java.lang.Class.newInstance(Native Method)
在 android.app.ActivityThread.handleCreateService(ActivityThread.java:3386) 在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)
在android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.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;
答案 0 :(得分:0)
这使用了这个:
public String getBatteryLevel(){ Intent batteryIntent = registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1); int scale = batteryIntent.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);