Is it possible to append a value to R.string or any resource under R.?

by David » Sat, 24 Oct 2009 10:01:51 GMT


Sponsored Links
 Screen A permits the user to input a value into an EditText field.
Screen B populates a TextView using one of the entries in strings.xml
based upon the TextView value.  For example, if the user inputs "2" on
Screen A then Screen B should populate the TextView with
R.strings.h2.  I have tried the following:

     // get the bundle extras from Screen A's intent
     Bundle extras = getIntent().getExtras();
     // pull out the value from the UserInput EditText sent from
Screen A
     Str desiredString = extras != null ? extras.getString
("UserInput") : "";
     // popluate textView with the string R.string.h + whatever the
user put on Screen A
     textView.setText(R.string.h + desiredString);

I get a "cannot resolve R.string.h resource" error message because,
evidently, the desiredString value is not appended onto R.string.h.
So, I decided to come at it from another angle:

     switch (desiredString) {
     case 1:
          textView.setText(R.string.h1);
     case 2:
          textView.setText(R.string.h2);
     . . .
     case 312:
          textView.setText(R.string.h312);

Note that you cannot switch on a string so I tried Integer.parseInt on
the string but I wind up with a blank Screen B with the switch
statement above.  It seems that the string is not turned into an int.
So, my question is twofold: (1) is it possible to append a variable
onto a R. entry and (2) if I am obliged to use the larger and uglier
switch approach, how do I turn an EditText string value into an Int
value?  Thanks.

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Crashed Service not restarting - !!! FAILED BINDER TRANSACTION !!! - Dianne could you help ?

Hi again,

Our service is still crashing for some reason not related to our
application. To make sure it's not RunTime exception we've used a :
try
{
  // Get WakeLock
  // Perform actions
}
catch (Throwable t)
{
  // log error (if any)
}
finally
{
  // Schedule a new service startup and PendingIntent an AlarmManager
  long tick = 60000L;
  long nextUpdate = System.currentTimeMillis();
  nextUpdate += (tick - nextUpdate % tick);
  Intent updateIntent = new Intent();
  updateIntent.setClass(this, RefreshService.class);
  PendingIntent pendingIntent = PendingIntent.getService(this, 0,
updateIntent, 0);
  AlarmManager alarmManager = (AlarmManager)getSystemService
(Context.ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC, nextUpdate, pendingIntent);
  // Release WakeLock
  ...
}
stopSelf();

The service is still crashing randomly on real device (Android 1.6/HTC
Magic) and it does not restart. It's not a problem of Taskiller
because it is not installed.
Here are the logs:
01-06 21:18:00.653: INFO/ActivityManager(75): Stopping service:
xx.yy/.widget.RefreshService
01-06 21:18:06.283: DEBUG/dalvikvm(1318): GC freed 4957 objects /
283104 bytes in 110ms
// Problem starts here.
01-06 21:18:59.153: INFO/ActivityManager(75): Process xx.yy(pid 1318)
has died.
01-06 21:21:33.530: INFO/ActivityManager(75): Start proc xx.yyfor
service xx.yy/.widget.RefreshService: pid=1339 uid=10031 gids={3003,
1015}
01-06 21:21:33.720: INFO/dalvikvm(1339): De{*filter*} thread not active,
ignoring DDM send (t=0x41504e4d l=38)
01-06 21:21:33.750: INFO/dalvikvm(1339): De{*filter*} thread not active,
ignoring DDM send (t=0x41504e4d l=48)
01-06 21:21:34.520: INFO/ActivityManager(75): Stopping service:
xx.yy/.widget.RefreshService
01-06 21:23:11.526: ERROR/JavaBinder(75): !!! FAILED BINDER
TRANSACTION !!!
01-06 21:23:11.536: ERROR/JavaBinder(75): !!! FAILED BINDER
TRANSACTION !!!
01-06 21:23:11.635: INFO/ActivityManager(75): Process xx.yy(pid 1339)
has died.
01-06 21:23:11.686: WARN/ActivityManager(75): Scheduling restart of
crashed service xx.yy/.widget.RefreshService in 5000ms
01-06 21:23:16.736: INFO/ActivityManager(75): Start proc xx.yy for
service xx.yy/.widget.RefreshService: pid=1349 uid=10031 gids={3003,
1015}
01-06 21:23:16.825: INFO/dalvikvm(1349): De{*filter*} thread not active,
ignoring DDM send (t=0x41504e4d l=38)
01-06 21:23:16.855: INFO/dalvikvm(1349): De{*filter*} thread not active,
ignoring DDM send (t=0x41504e4d l=48)
// But service never restarts !

We've run the following command according to Dianne advice: "adb shell
dumpsys activity.services" and we can see our service but it is not
executed.

Currently running services:
  activity.services
-------------------------------------------------------------------------------
DUMP OF SERVICE activity.services:
Services in Current Activity Manager State:
  Active services:
  * ServiceRecord{43563d78 xx.yy/.widget.RefreshService}
    intent={cmp=xx.yy/.widget.RefreshService}
    packageName=xx.yy
    processName=xx.uu
    baseDir=/data/app/xx.yy.apk/data/app/xx.yy.apk dataDir=/data/data/
xx.yy
    app=ProcessRecord{4348d8f8 1349:xx.yy/10031}
    isForeground=false lastActivity=-675021
    startRequested=true startId=1 executeNesting=0
executingStart=-675021 crashCount=0
    totalRestartCount=1 restartCount=1 restartDelay=5000
restartTime=-675021 nextRestartTime=-675183

  * ServiceRecord{43471978
com.google.android.location/.NetworkLocationService}
    intent={cmp=com.google.android.location/.NetworkLocationService}
    packageName=com.google.android.location
    processName=system
    baseDir=/system/app/NetworkLocation.apk/system/app/
NetworkLocation.apk dataDir=/data/data/com.google.android.location
    app=ProcessRecord{433869d8 75:system/1000}
    isForeground=false lastActivity=-6811360
    startRequested=true startId=1 executeNesting=0
executingStart=-6811353 crashCount=0
    totalRestartCount=0 restartCount=0 restartDelay=0
restartTime=-6811360 nextRestartTime=-6857706

  * ServiceRecord{43430480
com.google.android.googleapps/.GoogleLoginService}
    intent={cmp=com.google.android.googleapps/.GoogleLoginService}
    packageName=com.google.android.googleapps
    processName=com.google.process.gapps
    baseDir=/system/app/GoogleApps.apk/system/app/GoogleApps.apk
dataDir=/data/data/com.google.android.googleapps
    app=ProcessRecord{4337a268 149:com.google.process.gapps/10015}
    isForeground=false lastActivity=-330241
    startRequested=false startId=0 executeNesting=0
executingStart=-6812034 crashCount=0
    totalRestartCount=0 restartCount=0 restartDelay=0
restartTime=-6816373 nextRestartTime=-6857714

    * IntentBindRecord{4345a4d0}:
      intent={act=android.accounts.IAccountsService
cmp=com.google.android.googleapps/.GoogleLoginService}
      binder=android.os.binderpr...@4345adc8
      requested=true received=true hasBound=false doRebind=false

    * IntentBindRecord{43430848}:
      intent={cmp=com.google.android.googleapps/.GoogleLoginService}
      binder=android.os.binderpr...@43438fa0
      requested=true received=true hasBound=true doRebind=false

      * Client AppBindRecord{43430918 ProcessRecord{4337a268
149:com.google.process.gapps/10015}}
        Per-process Connections:
          ConnectionRecord{43398190
com.google.android.googleapps/.GoogleLoginService:@433a3768}
          ConnectionRecord{433b9908
com.google.android.googleapps/.GoogleLoginService:@433b9878}
          ConnectionRecord{433e7338
com.google.android.googleapps/.GoogleLoginService:@433e2100}

    All Connections:
      ConnectionRecord{433e7338
com.google.android.googleapps/.GoogleLoginService:@433e2100}
      ConnectionRecord{433b9908
com.google.android.googleapps/.GoogleLoginService:@433b9878}
      ConnectionRecord{43398190
com.google.android.googleapps/.GoogleLoginService:@433a3768}

  * ServiceRecord{43340138
com.google.android.server.checkin/.CheckinService}
    intent={cmp=com.google.android.server.checkin/.CheckinService}
    packageName=com.google.android.server.checkin
    processName=system
    baseDir=/system/app/GoogleCheckin.apk/system/app/GoogleCheckin.apk
dataDir=/data/data/com.google.android.server.checkin
    app=ProcessRecord{433869d8 75:system/1000}
    isForeground=false lastActivity=-6827000
    startRequested=true startId=1 executeNesting=0
executingStart=-6826997 crashCount=0
    totalRestartCount=0 restartCount=0 restartDelay=0
restartTime=-6827000 nextRestartTime=-6857736

  * ServiceRecord{4346d038 com.google.android.apps.gtalkservice/
com.google.android.gtalkservice.service.GTalkService}
    intent={cmp=com.google.android.apps.gtalkservice/
com.google.android.gtalkservice.service.GTalkService}
    packageName=com.google.android.apps.gtalkservice
    processName=com.google.process.gapps
 
permission=com.google.android.gtalkservice.permission.GTALK_SERVICE
    baseDir=/system/app/gtalkservice.apk/system/app/gtalkservice.apk
dataDir=/data/data/com.google.android.apps.gtalkservice
    app=ProcessRecord{4337a268 149:com.google.process.gapps/10015}
    isForeground=false lastActivity=-6811451
    startRequested=true startId=1 executeNesting=0
executingStart=-6811446 crashCount=0
    totalRestartCount=0 restartCount=0 restartDelay=0
restartTime=-6811451 nextRestartTime=-6857744

  * ServiceRecord{433c6970 com.android.phone/.BluetoothHeadsetService}
    intent={cmp=com.android.phone/.BluetoothHeadsetService}
    packageName=com.android.phone
    processName=com.android.phone
    baseDir=/system/app/Phone.apk/system/app/Phone.apk dataDir=/data/
data/com.android.phone
    app=ProcessRecord{4335dba0 118:com.android.phone/1001}
    isForeground=false lastActivity=-6822206
    startRequested=true startId=1 executeNesting=0
executingStart=-6006342 crashCount=0
    totalRestartCount=0 restartCount=0 restartDelay=0
restartTime=-6822206 nextRestartTime=-6857753

    * IntentBindRecord{43465dc8}:
      intent={act=android.bluetooth.IBluetoothHeadset}
      binder=android.os.binderpr...@434660d8
      requested=true received=true hasBound=false doRebind=false
      * Client AppBindRecord{434f1f50 ProcessRecord{4335dba0
118:com.android.phone/1001}}
        Per-process Connections:
          ConnectionRecord{434f2020
com.android.phone/.BluetoothHeadsetService:@434f1dd0}

    All Connections:
      ConnectionRecord{434f2020
com.android.phone/.BluetoothHeadsetService:@434f1dd0}

  * ServiceRecord{4323e190 com.android.inputmethod.latin/.LatinIME}
    intent={act=android.view.InputMethod
cmp=com.android.inputmethod.latin/.LatinIME}
    packageName=com.android.inputmethod.latin
    processName=com.android.inputmethod.latin
    permission=android.permission.BIND_INPUT_METHOD
    baseDir=/system/app/LatinIME.apk/system/app/LatinIME.apk dataDir=/
data/data/com.android.inputmethod.latin
    app=ProcessRecord{4342f8f8 285:com.android.inputmethod.latin/
10006}
    isForeground=false lastActivity=-6737471
    startRequested=false startId=0 executeNesting=0
executingStart=-6737464 crashCount=0
    totalRestartCount=0 restartCount=0 restartDelay=0
restartTime=-6737471 nextRestartTime=-6857768

    * IntentBindRecord{433f1c80}:
      intent={act=android.view.InputMethod
cmp=com.android.inputmethod.latin/.LatinIME}
      binder=android.os.binderpr...@43385cd8
      requested=true received=true hasBound=true doRebind=false

      * Client AppBindRecord{4329e058 ProcessRecord{433869d8 75:system/
1000}}

        Per-process Connections:
          ConnectionRecord{43294410
com.android.inputmethod.latin/.LatinIME:@43338040}

    All Connections:
      ConnectionRecord{43294410
com.android.inputmethod.latin/.LatinIME:@43338040}

  Connection bindings to services:

  * ConnectionRecord{434f2020
com.android.phone/.BluetoothHeadsetService:@434f1dd0}
    binding=AppBindRecord{434f1f50
com.android.phone/.BluetoothHeadsetService:com.android.phone}
    activity=HistoryRecord{43408018 com.android.phone/.InCallScreen}
    conn=android.os.binderpr...@434f1dd0 flags=0x0

  * ConnectionRecord{43294410
com.android.inputmethod.latin/.LatinIME:@43338040}
    binding=AppBindRecord{4329e058
com.android.inputmethod.latin/.LatinIME:system}
    conn=android.app.ActivityThread$PackageInfo$ServiceDispatcher
$innerconnect...@43338040 flags=0x1

  * ConnectionRecord{433e7338
com.google.android.googleapps/.GoogleLoginService:@433e2100}
    binding=AppBindRecord{43430918
com.google.android.googleapps/.GoogleLoginService:com.google.process.gapps}
    conn=android.os.binderpr...@433e2100 flags=0x1

  * ConnectionRecord{433b9908
com.google.android.googleapps/.GoogleLoginService:@433b9878}
    binding=AppBindRecord{43430918
com.google.android.googleapps/.GoogleLoginService:com.google.process.gapps}
    conn=android.os.binderpr...@433b9878 flags=0x1

  * ConnectionRecord{43398190
com.google.android.googleapps/.GoogleLoginService:@433a3768}
    binding=AppBindRecord{43430918
com.google.android.googleapps/.GoogleLoginService:com.google.process.gapps}
    conn=android.os.binderpr...@433a3768 flags=0x1

If we run the same command 2 hours later it returns for our service:

DUMP OF SERVICE activity.services:
Services in Current Activity Manager State:
  Active services:
  * ServiceRecord{43563d78 xx.worldwidyyetime/.widget.RefreshService}
    intent={cmp=xx.yy/.widget.RefreshService}
    packageName=xx.yy
    processName=xx.yy
    baseDir=/data/app/xx.yy.apk/data/app/xx.yy.apk dataDir=/data/data/
xx.yy
    app=ProcessRecord{4348d8f8 1349:xx.yy/10031}
    isForeground=false lastActivity=-5474190
    startRequested=true startId=1 executeNesting=0
executingStart=-5474190 crashCount=0
    totalRestartCount=1 restartCount=1 restartDelay=5000
restartTime=-5474190 nextRestartTime=-5474352

What could be the problem ?

Thanks for you help.















> > > > > > > 

2. I don't see how to do this....

I want to grab the latitude and longitude of a phone without using a
mapview (I want to send the location to a server side process from a
form).... But this code constantly crashes the app :

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(criteria,
true);
        Location loc = locationManager.getLastKnownLocation(provider);
        double lat = loc.getLatitude();

3. A log collector - Bug collector per android developers

4. SAXParser invalid token exception because of '&' inside attribute value

5. Question about emulator and accented character input

6. Interface fo wi-fi

7. Adding a customized layer to the Maps application