1

I make an app with background services and broadcast receiver, and my app consists of a MainActivity.java and myServiceRunner.java. When I start the background service, it gives me this error, android.content.res.Resources$NotFoundException: String resource id #0x64.

I put these codes inside protected void onCreate(Bundle savedInstanceState):

     IntentFilter mainFilter=new IntentFilter("liren.action.GOSERVICE3");
      receiver=new MyMainLocalReceiver();
      registerReceiver(receiver, mainFilter);

MyMainLocalReceiver.java

   public class MyMainLocalReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        try
        {
            int serviceData=intent.getIntExtra("serviceData",0);
            //Toast.makeText(getApplicationContext(), serviceData,Toast.LENGTH_SHORT).show();

            tvStepsCount.setText(serviceData);
        }
        catch (Exception e)
        {
            tvStepsCount.setText(e.toString());
        }

    }
}

Inside myServiceRunner extends Service class, I have added these codes inside onStartCommand method.

  @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
    Thread triggerService=new Thread(new Runnable() {
        @Override
        public void run() {
            for(;;)
            {
                try
                {
                    Intent myFilteredResponse = new Intent("liren.action.GOSERVICE3");
                    myFilteredResponse.putExtra("serviceData",100);
                    sendBroadcast(myFilteredResponse);
                    Thread.sleep(5000);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    });
    triggerService.start();
    return super.onStartCommand(intent, flags, startId);
}

manifest.xml

I have added these:

    <service android:name=".myServiceRunner"></service>
    <receiver android:name=".RecordSaved$MyMainLocalReceiver"></receiver>

I could not figure out the error, can anyone help me please? Thanks :)

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48

3 Answers3

8

You are using .SetText(). but you are passing int type. that's a problem. so you need to convert your int to String

Try like,

tvStepsCount.setText("" + serviceData); // concat with empty string
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
1

As you can see in the TextView class, first argument to TextView.setText() method can be either a resId, ie an integer or char sequence or char array. Therefore android studio will not show error or warning while you pass an Integer value to TexView.setText().

In your code tvStepsCount.setText(serviceData); serviceData is an integer type, android treat this value as a resId and look for the corresponding resource in R class. Since serviceData is not a resId actually android return Resources$NotFoundException.

So in this case we need to explicitly convert serviceData to String type.

shijo
  • 890
  • 7
  • 8
0

You can do this too:

 tvStepsCount.setText(String.valueOf(serviceData));
Nishan Khadka
  • 385
  • 1
  • 2
  • 14