Android string resource not accessable: android.co

2019-09-10 09:40发布

问题:

This question already has an answer here:

  • Why can't I use Resources.getSystem() without a Runtime error? 3 answers

I tried to get all my strings into that strings.xml file. Now if I try to run it, i get this:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: de.hsd.ip2.wgapp, PID: 20467
              android.content.res.Resources$NotFoundException: String resource ID #0x7f060061
                  at android.content.res.Resources.getText(Resources.java:321)
                  at android.content.res.Resources.getString(Resources.java:407)
                  at de.hsd.ip2.wgapp.Firebase.DatabaseContact.addRoom(DatabaseContact.java:215)
                  at de.hsd.ip2.wgapp.DialogfragmentCleaningSchedule$5.onDataChange(DialogfragmentCleaningSchedule.java:242)
                  at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                  at com.google.android.gms.internal.zzajp.zza(Unknown Source)
                  at com.google.android.gms.internal.zzakp.zzcxi(Unknown Source)
                  at com.google.android.gms.internal.zzaks$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5294)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

this is my strings.xml:

 <resources>
 [...]
 <!-- Firebase Strings -->
 <string name="fLat">Flat</string>
 [...]
 </resources>

The R file has this entry for that string:

public static final int fLat=0x7f060061;

and if I rightklick on any R.strings.fLat and use "Find usages" I see the right fLat at Attribute value:

Oh and the point where I try to get it, I also try to get another string and that one works, I even renamed flat to fLat, but it just doesn't work..

fReference.child(Resources.getSystem().getString(R.string.fLat)).child(Integer.toString(flatId))
            .child(Resources.getSystem().getString(R.string.cleaning))
            .child(title)
            .setValue(r)

I get the R.string.cleaning without a Problem..

Thanks in advance for any Idea :)

回答1:

You can´t access your resources with Resources.getSystem(). Like defined in API:

Return a global shared Resources object that provides access to only system resources (no application resources)

instead you should use something like

context.getResources().getString(R.string.fLat),

or if you are inside an activity:

getResources().getString(R.string.fLat).

So your code must be:

fReference.child(getResources().getString(R.string.fLat)).child(Integer.toString(flatId))
            .child(getResources().getString(R.string.cleaning))
            .child(title)
            .setValue(r)