Android [string array] cannot be resolved or is not a field - java

I'm getting an error about a string array (located in strings.xml) not being able to be resolved.
I've done everything the internet says:
Restarted Eclipse
Deleted my R.java file inside the gen folder
My values folder is still inside the res folder, not moved
None of my XML files have erros
Ran Project -> Clean
I don't have android.R imported in my class.
Checked the R.java class, it indeed doesn't have an app_categories property (my array's name)
The relevant part of the XML:
<string-array name="app_categories">
<item >Cat 1</item>
<item >Cat 2</item>
<item >Cat 3</item>
<item >Cat 4</item>
<item >Cat 5</item>
<item >Cat 6</item>
<item >Cat 7</item>
<item >Cat 8</item>
<item >Cat 9</item>
</string-array>
The Java code:
String[] categoryNames = getResources().getStringArray(R.string.app_categories);
All my other strings are visible. I have another array defined just like this one, and that one isn't visible either.
Exact error below:
app_categories cannot be resolved or is not a field [class_name].java [path] line 45 Java Problem

Just try out this way :
getResources().getString(R.array.app_categories_list);
Instead of R.string use R.array you will get your arraylist.

Don't put your string arrays in strings.xml You must create a new file called arrays.xml and place them there.
And when calling you need R.arrays.myarray, not R.strings.MyArray

After cleaning your project try restarting the adb server by running adb kill-server and adb start-server from the command line

a solution may be to create the R manually:
just hover over the error in the activity file where the problem was and then manually create it, which will cause R to generate the string array you created in strings.xml

Related

Vector drawables not loading in xml ResourceNotFoundException

I am having trouble with loading vector files in my splash background xml file. The vector has been loaded successfully because I can open the vector file in android studio and I get a small graphic of the vector on the side of the xml file. However, the design view of the xml file shows an error for the vector file, and I get a ResourceNotFoundException when trying to run my app.
Code below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_loading_screen_logo"/>
</item>
</layer-list>
splash background.xml
My grade version is classpath 'com.android.tools.build:gradle:4.0.1'
and I have included the following line in my grade app file vectorDrawables.useSupportLibrary = true
It seems that any vector file that I use has this error, so there is some setting of some sort that is missing.
Please assist in anyway.
Thanks,
The problem is wrapping the xml drawable in a <bitmap/>
Try this:
Replace
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_loading_screen_logo"/>
</item>
With:
<item android:gravity="center"
android:drawable="#drawable/ic_loading_screen_logo"/>
replace <bitmap to <item
make sure that your vector is in drawable folder not in drawable-24.
check vectory path may be its too long.

Android not recognizing string resource

In my android app, I have a string
<string name="action_upload">Upload Image</string>
in the strings file. Then in a menu file I have
navigation_screen.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="android.arin.NavigationScreen" >
<item
android:id="#+id/action_upload"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_upload"/>
<item
android:id="#+id/action_forward"
android:icon="#drawable/ic_action_forward"
android:title="#string/action_forward"
android:showAsAction="ifRoom" />
</menu>
However when I try to run it, I get an error saying:
[2014-06-19 17:26:18 - ARIN] W/ResourceType( 9060): ResXMLTree_node size 0 is smaller than header size 0x100.
[2014-06-19 17:20:26 - ARIN] C:\Users\NAME\Documents\NAME\NAME\ARIN\res\menu\navigation_screen.xml:5: error: Error: No resource found that matches the given name (at 'title' with value '#string/action_upload').
I even tried a Project clean in eclipse, but still problem persists.
Does anyone know how to fix it?
Thanks
This may seem like a weird answer, but did you save the xml file. Hit Ctrl-S and try it again.
If that doesn't work: Are the other strings you declared in the strings.xml file visible?

Having an error in my strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DataGush Alpha</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="menu_items">
<item >Top Lists</item>
<item >Map</item>
<item >Search</item>
</string-array>
</resources>
I am getting these errors:
[2014-03-02 15:31:32 - DataGushAlpha] W/ResourceType(64288): ResXMLTree_node header size 0 is too small.
[2014-03-02 15:31:32 - DataGushAlpha] C:\Users\anshv_000\workspace\DataGushAlpha\res\values\strings.xml:12: error: Error parsing XML: mismatched tag
[2014-03-02 15:31:32 - DataGushAlpha] C:\Users\anshv_000\workspace\DataGushAlpha\res\menu\main.xml:3: error: Error: No resource found that matches the given name (at 'title' with value '#string/action_settings').
Try to check the XML file in a simple text editor for errors.
Sometimes when you let Eclipse/ADT autocomplete inline it just leaves some garbage there that is not visible in the editor somehow. It happens frequently to me.
I have had some problems with incorrect new line characters sometimes, please check the file and ensure that the xml is correct.
You should see whats wrong in the string.xml file in eclipse, shows up as a error in the editor or you could look in the problems view. If the error is not shown in the string.xml file then it's not a problem with the file, in this case the R file hasn't been updated correctly and therefore the string resource cannot be found, this happens for me from time to time. The solution to this is usually to clean and build the project, alternatively close and open the project.
Try to delete the space between item and >
<item>Top Lists</item>
<item>Map</item>
<item>Search</item>
and check this tag 'action_settings': there is no String with this tag

How to read xml file in android?

I've the following xml file inside the /mnt/sdcard. I want to get the version of the item type=provider from the following file. The file is big(500 lines) which has othere types also. This is simplified file. In the file I'm intetersted in this node:
<Item Type="Provider" Version="19.0.0.0"Checksum="EShHVeNtW1xTfEvLvATwqA==" FileSize="2746200" />
From this node I want to get the version i.e. 19.0.0.0.
Here is my xml file:
<Manifest guid="FD29E1EF-A5C4-4D19-ACC8-8C98C7E91B02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PackageType="Full" Scope="APPROVED">
<Items>
<Item id="fcxm-8ikj-olk-ffgcxh3" Checksum="EShHVeNtW1xTfEvLvATwqA==" value="f425921f-b6ef-4e58-8a14-fcbd0d7e50e9" />
<Item Type="question" Version="19.0.0.0"Checksum="EShHVeNtW1xTfEvLvATwqA==" FileSize="2746200" />
<Item Type="Provider" Version="19.0.0.0"Checksum="EShHVeNtW1xTfEvLvATwqA==" FileSize="2746200" />
</Items>
</Manifest>
I'm using Java code to develop my android application. I searched on the internet, I got this which is iterating to all the nodes of item type. I dont want want to iterate.
in android we have three normal way to parse xml, XmlPullParser, SaxXmlPullParser, DocumentBuilder(DOM parser), and android use XmlPullParser for android resource parser.

How to set the Default Value of a ListPreference

i need to set the defult value for a ListPreference when the Activity starts.
I've tried with ListPreference.setDefaultvalue("value"); but it makes the firts Entry of the List as default. I need it because i must check a condition and set as default the value which satisfies that condition, so I think it can't be done from the xml file (with android:defaultValue)
For example, suppose I have this array of values in the arrays.xml:
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
In the PreferenceScreen xml:
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="#array/opts"
android:entryValues="#array/opts_values" />
In the Activity I'd like to do something like this:
String mycolour;
if (something) {
mycolour="1";
} else {
mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);
But it doesn't work, because it makes the first choice as default. Could you explain me how to make another one as default? Thanks.
You don't need to programmatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
...
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="#array/opts"
android:entryValues="#array/opts_values"
android:defaultValue="2" />
here I selected 2 as a default value. Remember defaultvalue will be opts_values element.
Have you tried:
setValueIndex(int index);
Sorry my bad English.
List item
Retrieve the list Check if the value is null. If it is null set to the default value.
Code:
ListPreference dataPref = (ListPreference) findPreference("keyList");
if(dataPref.getValue() == null){
dataPref.setValueIndex(0); //set to index of your deafult value
}
You can set your default value by using the key like this
<string-array name="syncFrequency">
<item name="1">Block All Calls</item>
<item name="2">Block Black List</item>
<item name="3">Block Unknown Calls</item>
<item name="4">Allow White List</item>
<item name="5">Receive All Calls</item>
</string-array>
<string-array name="syncFrequencyValues">
<item name="1">Block_All_Calls</item>
<item name="2">Block_Black_List</item>
<item name="3">Block_Unknown_Calls</item>
<item name="4">Allow_White_List</item>
<item name="5">Receive_All_Calls</item>
</string-array>
<ListPreference
android:key="prefSyncFrequency"
android:entries="#array/syncFrequency"
android:summary="%s"
android:defaultValue="Block_Black_List"
android:entryValues="#array/syncFrequencyValues"
android:title="#string/call_block_options" />
or you can also try colour.setValue(mycolour);
Just for the record if someone else has this problem:
setValueIndex(int X) is setting the value # index X to the default value - so it is probably what you are looking for.
Set this value AFTER you added the Values! (stupid mistake but took me half an hour)
Actually it's because the SharedPreferences will persist after you re-build the app.
Uninstall it and try again.
((ListPreference) findPreference("pref_language")).setValue(Locale
.getDefault().getLanguage());
setValue() is ListPreference's method, and setDefaultvalue is Preference's method
This is an old post, but here's another way to set the default value for ListPreference with the following line of code:
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.
Note: the <VALUE> is the actual value and not the index of string
array.
If still its not working, try below steps.
Clear application data.
Uninstall and reinstall the app
Check the list preference, you will see the default value selected
Strange, I know but it worked in my case.
If you are using Android Jetpack Preference, known as androidx.preference:preference-ktx:1.1.1 in Kotlin, you can use:
app:defaultValue="<Value_in_string-array_with_values>".
Additionally: defaultValue is not an index number, is the actual value from the values array.
I also recommend using a string resource for the default value and clearing the data, uninstalling the app, or removing the file:
<package_name_in_app_manifest>_preferences.xml in data/data/shared_prefs/<package_name_in_app_manifest>. Replace <package_name_in_app_manifest> with real name like com.example.yourapp.
I was having the same issue and the defaultValue wasn't updating because it already had a wrong default value of "true". Solved it by using the Android Studio File Explorer and deleting the file.
And here is my solution example:
res/xml/root_preferences.xml
<ListPreference
app:key="mic"
app:title="#string/select_mic_title"
app:useSimpleSummaryProvider="true"
app:entries="#array/mic_entries"
app:entryValues="#array/mic_values"
app:defaultValue="#string/default_mic"
 />
res/values/arrays
<!-- Mic Preference -->
<string-array name="mic_entries">
<item>#string/default_mic</item>
<item>Headband</item>
</string-array>

<string-array name="mic_values">

 <item>#string/default_mic</item>
<item>Headband</item>

</string-array>
res/strings.xml
<string name="default_mic">Integrated</string>
Result:
Setting Fragment with default list value of "Integrated" given by string resource #string/default_mic

Categories