I am trying to get battery information through the use of a broadcast receiver and store it in a database. I'd prefer to get it only when I specifically want it, but I am willing to keep a database of just running records of it. Anyways, The problem is my app crashes with this error:
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) }
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4918)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f04006b
at android.content.res.Resources.getText(Resources.java:242)
at android.widget.TextView.setText(TextView.java:3773)
at com.Eddiecubed44.drunk.buddy.Main$2.onReceive(Main.java:180)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
And when i say crash, I mean crash and burn. My phone restarts itself every time I run it with this code in it.
I've stepped through with the debugger and haven't caught the error. I create the broadcast Receiver in my main activity class as so:
BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int temp = -1;
TextView tempText;
tempText = (TextView)findViewById(R.id.mybatttemptxt);
temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
tempText.setText(R.string.temp + temp);
This is how im registering the Broadcast Receiver.
this.registerReceiver(this.batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
I do this in my onStart Method. The thing is, I can run my app if i replace this.batteryReceiver with null, but the app does nothing. The receiver is not called or used anywhere else in this app.
If it matters heres what I'm using: testing on rooted galaxy s3 app is using target lvl15 api min 11.
You've got an inexistent Resource in line 180 of your Main.java.
My guess is it's R.string.temp
but since you haven't posted even the name of your file, it's just that: a guess.
And I just saw the trouble:
tempText.setText(R.string.temp + temp);
The documentation allows setting the text directly to a resid. However, by adding temp to that value, you altered it and are asking for an inexistent resource.
One way to correct it would be to:
String resourceTemp = context.getString(R.string.temp);
tempText.setText(resourceTemp + " " + temp);