3

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.

eddiecubed
  • 174
  • 1
  • 2
  • 12
  • 1
    post your manifest permission – Hoan Nguyen Apr 10 '13 at 23:36
  • 1
    Concerning @HoanNguyen's comment: it's more likely that you're referencing an element in your layout incorrectly. But to know that, you'd need to post a bigger excerpt both of your logcat and the code itself. – DigCamara Apr 10 '13 at 23:43
  • My uses-permissions or my activity and intent-Filter and what not? – eddiecubed Apr 10 '13 at 23:50
  • Your whole receiver code. – Hoan Nguyen Apr 10 '13 at 23:53
  • I don't register it in the manifest, just in the onStart Method. – eddiecubed Apr 10 '13 at 23:56
  • Yes but your code for batteryReceiver has some error or you misses some permission. – Hoan Nguyen Apr 10 '13 at 23:57
  • It look like //post some info into textViews is where the error is. – Hoan Nguyen Apr 10 '13 at 23:58
  • Hi! StackOverflow is for programming questions, and you have not asked a question. Your stack trace shows you where your code has an issue, and you did not post that code, so it will be difficult for anyone to help you. – CommonsWare Apr 11 '13 at 00:01
  • Ive added some code in. and I don't have any permissions related to the Battery receiver in my manifest. – eddiecubed Apr 11 '13 at 00:04
  • The line 180 happens right on the 'temp = intent.putIntExtra(' line. – eddiecubed Apr 11 '13 at 00:07
  • 1
    "The line 180 happens right on the 'temp = intent.putIntExtra(' line." -- no, it does not. The stack trace clearly shows that your error is coming from a call to `setText()`. Whether that is the `setText()` call in your abbreviated code listing or not, only you can determine. – CommonsWare Apr 11 '13 at 00:10

2 Answers2

3

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);
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • So I'm assuming if I decide to write it as a single string and not use R.string.temp and include "script" instead. – eddiecubed Apr 11 '13 at 00:13
1

You add a resource id to temp and thus change the id, you should change to

tempText.setText(context.getString(R.string.temp) + temp);
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • holy stuff that I can't talk about here (i dont think) that made it all better. Thanks. I was not expecting it to be a elementary mistake like this. – eddiecubed Apr 11 '13 at 00:15
  • @DigCamara I did not pay attention didn't I? Well, your answer is a plus anyway. – Hoan Nguyen Apr 11 '13 at 00:18