Good day, Any idea why i could be getting a NullPointerException in the following code. I am trying to retrieve elements from a string-array in XML and test against a given value but i keep getting a NullpointerException on the first line of the code below.
String [] position = context.getResources().getStringArray(R.array.google_weather_conditions); // NullpointerException here
for(String check: position){
String test_condition = check;
if(this.condition.equals(test_condition)){
weather_conditions = test_condition;
}
}
here is the strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Clear">Clear</string>
<string name="Sunny">Sunny</string>
<string name="Mostly_Sunny">Mostly Sunny</string>
<string name="Partly_Sunny">Partly Sunny</string>
<string name="Cloudy">Cloudy</string>
<string name="Mostly_Cloudy">Mostly Cloudy</string>
<string name="Partly_Cloudy">Partly Cloudy</string>
<string name="Rain">Rain</string>
<string-array name="google_weather_conditions">
<item>#string/Clear</item>
<item>#string/Sunny</item>
<item>#string/Mostly_Sunny</item>
<item>#string/Partly_Sunny</item>
<item>#string/Cloudy</item>
<item>#string/Mostly_Cloudy</item>
<item>#string/Partly_Cloudy</item>
<item>#string/Rain</item>
</string-array>
</resources>
Most likely context is null at the point of the NPE.
context.getResources().getStringArray(R.array.google_weather_conditions); // NullpointerException here
The following could be null:
context
return value of context.getResources()
R
R.array
R.array.google_weather_conditons
Time to either step through with a debugger, or break it down, line by line and see where it occurs.
What is R?
Related
I have array in file res/values/ids.xml
<array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
...
</array>
I tried to get these ids, but it didn't work(((
val typedArray = resources.obtainTypedArray(viewSetId)
val idSet = IntArray(typedArray.length())
for (i in 0 until typedArray.length()) {
idSet[i] = typedArray.getInt(i, 0)
}
typedArray.recycle()
All idSet's elements equals 0.
I also tried store integer array in res/values/integers.xml
<integer-array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
...
</integer-array>
And then get these ids
val idSet = resources.getIntArray(R.array.first_set)
But result is the same((
IntArray Xml should be like this Format
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="first_set">
<item>#id/layout1</item>
<item>#id/layout2</item>
<item>#id/layout3</item>
</integer-array>
</resources>
in the path xml resource in res/values/integers.xml
Access the xml data in the activity like this way
val arrayValues = resources.getIntArray(R.array.first_set)
Use #+id to ensure they are created by the time the array is generated:
<integer-array name="first_set">
<item>#+id/layout1</item>
<item>#+id/layout2</item>
<item>#+id/layout3</item>
...
</integer-array>
I have this file array.xml and I want to get the item value from an array.
How can I do that? I have tried with getInt but that returns 0. All help is welcome.
<resources>
<array name="firstAd">
<item>border_top_id_1v</item>
<item>R.id.dugme_1v</item>
<item>R.id.rent_or_buy_1v</item>
<item>R.id.currency_1v</item>
<item>R.id.price_1v</item>
<item>R.id.name_1v</item>
<item>R.id.address_1v</item>
</array>
</resources>
First, change each item to #id/object instead of R.id.object, then change the tag from array to integer-array, and move the code to your 'integer.xml' resource file.
integer.xml:
<resources>
<integer-array name="firstAd">
<item>#id/dugme_1v</item>
<item>#id/rent_or_buy_1v</item>
<item>#id/currency_1v</item>
<item>#id/price_1v</item>
<item>#id/name_1v</item>
<item>#id/address_1v</item>
</integer-array>
</resources>
Then, programmatically use a TypedArray, like so:
TypedArray firstAd = getResources().obtainTypedArray(R.array.firstAd);
int resourceId = firstAd.getResourceId(index, defValue);
try #id/yourIDHere
<item>border_top_id_1v</item>
<item>#id/dugme/1v</item>
<item>#id/rent_or_buy_1v</item>
<item>#id/currency_1v</item>
I am showing Random text in the two textview's on button click. Now I have to show the explaination of the text showed in textview1 in textview2.But I am not able to get it. Help will be appreciated.
I put the text in string.xml
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>
<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>
My code in MainActivity:
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(array_number[textview_number]);
number_explaination = new Random();
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number_explaination = number_explaination.nextInt(array_number_explaination.length - 1);
textView2.setText(array_number_explaination[textview_number_explaination]);
What I want here if I get Random R.string.two in my textview1 and then I will get R.string.two_explaination and so on.How can I achive this. Sorry for my bad english.
try this
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));
You do not need to generate another random number for explanation text
Try this:
textView2.setText(getString(array_number_explaination[textview_number_explaination]));
getString(int) should be used to get a string in your string resource file.
you need to use string array in the strings file :
<string-array
name="string_array_name">
<item
>text_string</item>
</string-array>
and then make an array in your code with reference of :
R.array.string_array_name
see http://developer.android.com/guide/topics/resources/string-resource.html for reference.
after you got the two arrays in your code you can run loops and do your desired work
If I have this string array:
<string-array name="htmlstrings">
<item><i>blah<br />blah</i></item>
<item><b>1st line<br />2nd line</b></item>
</string-array>
And I get it and set it as text:
String[] htmlstrings = getResources().getStringArray(R.array.htmlstrings);
textV.setText(Html.fromHtml(htmlstrings[1]));
As output I get:
1st line2nd line
But I want to get
1st line
2nd line
For regular strings (not string-array) I know I can get and display HTML with getText() like:
textV.setText(getText(R.string.onehtmlstring));
but I don't know what's the getText() equivalent for a string-array.
Use getTextArray() to make a CharSequence array, and set your TextView using that array.
CharSequence[] htmlchars = getResources().getTextArray(R.array.htmlstrings);
textV.setText(htmlchars[1]);
Have you try below code:-
Spanned sp = Html.fromHtml( getString(R.string.htmlsource));
tv.setText(sp);
or
Set TextView text from html-formatted string resource in XML
Try with CDATA attribute:
<string-array name="channel_link">
<item><![CDATA[https://news.google.com/news/feeds?pz=1&cf=all&ned=in&hl=en&output=rss]]></item>
</string-array>
I have this array into my resource file :
<array name="xml_data">
<item>#xml/data1</item>
<item>#xml/data2</item>
<item>#xml/data3</item>
<item>#xml/data4</item>
</array>
Normally, it's not different from an normal array, but when getting in code, this doesn't work...
final Resources res = getResources();
int[] xmlList = res.getIntArray(R.array.xml_data);
Log.i(TAG, "Data found: "+ xmlList.length);
for (int i = 0; i < xmlList.length; i++) {
Log.i(TAG, "Extract xml id="+ xmlList[i].);
}
Here is the output obtained in the logcat :
Data found: 4
Extract xml id=0
Extract xml id=0
Extract xml id=0
Extract xml id=0
Can you help me about this?
Thanks.
For your convenience lets take an example:
I had a simple 'string' array( not int) in a sample xml file. Lets call it array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="doing">
<item>1</item>
<item>2/item>
<item>3/item>
<item>3</item>
</string-array>
</resources>
Now in my Java file, I called it as follows inside my OnCreate() function and it worked:
String[] xmlList = getResources().getStringArray(R.array.doing);
int first = Integer.parseInt(xmlList[0]);
Log.d("test", "1st string is: " + first);
P.S: I haven't tested the code but I hope you got the logic. All the best. Hope it helps.
Use your TypedArray like this:
TypedArray mTypedArray = getResources().getTypedArray(R.array.xml_data);
Then, retrieve your resource ID's like this:
int id = mTypedArray.getResourceId(position, defaultResourceId);
When I used this, it was an array of String IDs, so I followed that with this:
getString(id)