I want to get the name of my string selected on a spinner.
This is my code to init the spinner;
exam_reason_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
exam_reason_adapter.add(getResources().getString(R.string.programmed));
exam_reason_adapter.add(getResources().getString(R.string.valve_installation));
exam_reason_adapter.add(getResources().getString(R.string.improvised));
reason_status.setAdapter(exam_reason_adapter);
This is how I implement it
reason_status.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
reason_checked = reason_status.getSelectedItem().toString();
int reasonID = getResources().getIdentifier(reason_checked, "string", null);
reason = getResources().getResourceName(reasonID).replaceAll("_", "-"); //I need the string with "-" to add it in my jsonRequest.
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
But when I launch my app , i just get this exception: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 All the strings values are defined.
EDIT : The Logcat
26413-26413/esisar.test.com.essai E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0
at android.content.res.Resources.getResourceName(Resources.java:1648)
at esisar.pi13.com.essai.HomeActivity$6.onItemSelected(HomeActivity.java:176)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
at android.widget.AdapterView.access$200(AdapterView.java:49)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
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:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The line 176 is this code:
reason = getResources().getResourceName(reasonID).replaceAll("_", "-");