Skip to content

Commit a3ec80f

Browse files
committed
fix string npe
1 parent cdabf77 commit a3ec80f

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/main/java/com/reandroid/arsc/array/StringArray.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.AbstractList;
2626
import java.util.ArrayList;
27+
import java.util.Iterator;
2728
import java.util.List;
2829

2930
public abstract class StringArray<T extends StringItem> extends OffsetBlockArray<T> implements JSONConvert<JSONArray> {
@@ -51,7 +52,7 @@ protected void onPostShift(int index){
5152
@Override
5253
protected void onPreRefreshRefresh(){
5354
if(isFlexible()){
54-
trimAllocatedFreeSpace();
55+
trimNullBlocks();
5556
}
5657
super.onPreRefreshRefresh();
5758
}
@@ -85,8 +86,10 @@ List<T> listUnusedStringsToRemove(){
8586
}
8687
public List<T> listUnusedStrings(){
8788
List<T> results=new ArrayList<>();
88-
for(T item:listItems()){
89-
if(!item.hasReference()){
89+
T[] childes = getChildes();
90+
for(int i = 0; i < childes.length; i++){
91+
T item = childes[i];
92+
if(item != null && !item.hasReference()){
9093
results.add(item);
9194
}
9295
}
@@ -96,12 +99,15 @@ public void setUtf8(boolean is_utf8){
9699
if(mUtf8==is_utf8){
97100
return;
98101
}
99-
mUtf8=is_utf8;
100-
T[] childes=getChildes();
102+
mUtf8 = is_utf8;
103+
T[] childes = getChildes();
101104
if(childes!=null){
102-
int max=childes.length;
103-
for(int i=0;i<max;i++){
104-
childes[i].setUtf8(is_utf8);
105+
int length = childes.length;
106+
for(int i=0; i<length; i++){
107+
T item = childes[i];
108+
if(item != null){
109+
item.setUtf8(is_utf8);
110+
}
105111
}
106112
}
107113
}
@@ -124,10 +130,9 @@ public JSONArray toJson(boolean styledOnly) {
124130
}
125131
JSONArray jsonArray=new JSONArray();
126132
int i=0;
127-
for(T item:listItems()){
128-
if(item.isNull()){
129-
continue;
130-
}
133+
Iterator<T> itr = iterator(true);
134+
while (itr.hasNext()){
135+
T item = itr.next();
131136
if(styledOnly && !item.hasStyle()){
132137
continue;
133138
}

src/main/java/com/reandroid/arsc/base/BlockArray.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ public final T get(int i){
322322
}
323323
return elementData[i];
324324
}
325+
public final T getLast(){
326+
return get(childesCount() - mFreeSpace - 1);
327+
}
325328
public int indexOf(Object block){
326329
T[] items=elementData;
327330
if(items==null){
@@ -354,9 +357,11 @@ public Iterator<T> iterator() {
354357
return iterator(false);
355358
}
356359
public Iterator<T> iterator(boolean skipNullBlock) {
360+
trimAllocatedFreeSpace();
357361
return new BlockIterator(skipNullBlock);
358362
}
359363
public Iterator<T> iterator(Predicate<T> tester) {
364+
trimAllocatedFreeSpace();
360365
return new PredicateIterator(tester);
361366
}
362367
public boolean contains(Object block){
@@ -457,6 +462,8 @@ private int countNonNull(boolean is_null_check){
457462
return result;
458463
}
459464
private void changeSize(int amount){
465+
mFreeSpace = 0;
466+
mAllocateStep = 0;
460467
T[] old=elementData;
461468
int index=old.length;
462469
int size=index+amount;

src/main/java/com/reandroid/arsc/pool/StringPool.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ public boolean contains(String str){
251251
public final T get(int index){
252252
return mArrayStrings.get(index);
253253
}
254+
public final T getLast(){
255+
return mArrayStrings.getLast();
256+
}
254257
public final StringGroup<T> get(String str){
255258
return mUniqueMap.get(str);
256259
}

0 commit comments

Comments
 (0)