私は夢の辞書を作っています.txtファイルから読み込んで、リストビューに表示するために配列に保存しています。しかし、log cat にはいくつかのエラーがあります。何が問題なのか教えてください。
05-07 14:29:06.750: E/AndroidRuntime(29135): FATAL EXCEPTION: main
05-07 14:29:06.750: E/AndroidRuntime(29135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sonovnik.petkovski/com.sonovnik.petkovski.Main}: java.lang.ArrayIndexOutOfBoundsException
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.os.Looper.loop(Looper.java:130)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.ActivityThread.main(ActivityThread.java:3835)
05-07 14:29:06.750: E/AndroidRuntime(29135): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:29:06.750: E/AndroidRuntime(29135): at java.lang.reflect.Method.invoke(Method.java:507)
05-07 14:29:06.750: E/AndroidRuntime(29135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-07 14:29:06.750: E/AndroidRuntime(29135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-07 14:29:06.750: E/AndroidRuntime(29135): at dalvik.system.NativeStart.main(Native Method)
05-07 14:29:06.750: E/AndroidRuntime(29135): Caused by: java.lang.ArrayIndexOutOfBoundsException
05-07 14:29:06.750: E/AndroidRuntime(29135): at com.sonovnik.petkovski.Main.readTxt(Main.java:115)
05-07 14:29:06.750: E/AndroidRuntime(29135): at com.sonovnik.petkovski.Main.initStuff(Main.java:131)
05-07 14:29:06.750: E/AndroidRuntime(29135): at com.sonovnik.petkovski.Main.onCreate(Main.java:39)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 14:29:06.750: E/AndroidRuntime(29135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
05-07 14:29:06.750: E/AndroidRuntime(29135): ... 11 more
コードは次のとおりです。
public class Main extends ListActivity {
private ArrayList<Son> sonovi;
private EditText filterText = null;
private SonovnikAdapter adapter;
private ListView list;
Translator t = new Translator();
private Intent intent;
private ArrayList<Son> temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initStuff();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
intent.putExtra("opis", sonovi.get(position).getOpis());
intent.putExtra("naslov", sonovi.get(position).getNaslov());
startActivity(intent);
}
});
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
String test = filterText.getText().toString().toUpperCase();
int dolzina = filterText.length();
temp = new ArrayList<Son>();
for (int i = 0; i < sonovi.size(); i++) {
if (filterText.getText().toString().toLowerCase().equals(
(String) sonovi.get(i).getLatinicno().toLowerCase()
.subSequence(0, dolzina))) {
temp.add(sonovi.get(i));
}
}
SonovnikAdapter testc = new SonovnikAdapter(Main.this,
R.layout.item, temp);
list.setAdapter(testc);
testc.notifyDataSetChanged();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
intent.putExtra("opis", temp.get(position).getOpis());
intent.putExtra("naslov", temp.get(position)
.getNaslov());
startActivity(intent);
}
});
}
});
}
private ArrayList<Son> readTxt() {
ArrayList<Son> s = new ArrayList<Son>();
InputStream is = this.getResources().openRawResource(R.raw.sonovnik);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
String naslov, opis, latinica;
String[] tmp;
try {
while ((str = br.readLine()) != null) {
tmp = str.split("-");
naslov = tmp[0].toString();
opis = tmp[1].toString();
latinica = tmp[2].toString(); //line 115
Log.v("test", latinica);
s.add(new Son(naslov, opis, latinica));
}
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
private void initStuff() {
list = getListView();
list.setTextFilterEnabled(true);
sonovi = new ArrayList<Son>();
sonovi = readTxt(); //line 131
intent = new Intent(this, Details.class);
adapter = new SonovnikAdapter(this, R.layout.item, sonovi);
setListAdapter(this.adapter);
filterText = (EditText) findViewById(R.id.search_box);
}
private class SonovnikAdapter extends ArrayAdapter<Son> {
private ArrayList<Son> items;
public SonovnikAdapter(Context context, int textViewResourceId,
ArrayList<Son> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
Son o = items.get(position);
if (o != null) {
TextView naslov = (TextView) v.findViewById(R.id.textView1);
if (naslov != null) {
naslov.setText(o.getNaslov().toString());
}
}
return v;
}
}
}