私は必死に次のようなバグを修正しようとしています:
- Androidバージョン2.2、2.3のエミュレータでは常に発生します
- エミュレータAndroidバージョン4では発生しません。*
- 実際のデバイスでは発生しません(android 4. *)
これは、次のIndexOutOfBoundsException例外です。
java.lang.RuntimeException: Unable to start activity
ComponentInfo{<myapppackage>}: java.lang.IndexOutOfBoundsException:
Invalid index 39, size is 0
私のアプリでは、テキストとして表示しているjsonファイルのデータに影響を与えています。私はバグがどこから来ているのかを特定しました、それは私がこのメソッドを呼び出すときです:
public String getItemValue(int id, String s) {
List<JsonItems> list = new ArrayList<JsonItems>();
try {
// CONVERT RESPONSE STRING TO JSON ARRAY
JSONArray ja = new JSONArray(s);
// ITERATE THROUGH AND RETRIEVE
int n = ja.length();
for (int i = 0; i < n; i++) {
// GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
JSONObject jo = ja.getJSONObject(i);
// RETRIEVE EACH JSON OBJECT'S FIELDS
JsonItems ji = new JsonItems();
ji.id = jo.getInt("id");
ji.text= jo.getString("text");
list.add(ji);
}
} catch (Exception e) {
e.printStackTrace();
}
return list.get(id).text;
}
私のクラスJsonItemsは非常に基本的です:
public class JsonItems{
int id;
String text;
}
私のjsonファイルからのサンプル:
[
{"id":0,"text":"some text 0"},
{"id":1,"text":"some text 1"},
{"id":2,"text":"some text 2"}
]
jsonファイルのコンテンツを文字列に処理する方法は次のとおりです
public static String fromJsonFileToString(String fileName, Context c) {
//JSONArray jArray = null;
String text = "";
try {
InputStream is = c.getAssets().open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
return text;
}
もう一度繰り返します。IndexOutOfBoundsExceptionはAndroid4.*を搭載したデバイスでは発生しません。これは、Android2.*を搭載したエミュレーターでアプリをテストした場合にのみ発生します。
それがどこから来ているのか考えていますか?
ありがとう