arrays.xml의 내용을 받아와서 리스트뷰로 각 행에 두 줄씩 출력되게 하려고 합니다.
표시될 리스트뷰는 21칸입니다.
(각 칸에 들어갈 내용은 values/arrays.xml에 첫째줄에 들어갈 내용을 menu1, 둘째줄에 들어갈 내용은 menu2로 적어두었습니다)
----------------아래는 MainActivity.java의 내용입니다.----------------
package com.dongdonge.achievement;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item;
item = new HashMap<String,String>();
String[] arMenu1 = getResources().getStringArray(R.array.menu1);
String[] arMenu2 = getResources().getStringArray(R.array.menu2);
for (int i=0;i<=21;i++)
{
item.put("item 1", arMenu1[i]);
item.put("item 2", arMenu2[i]);
list.add(item);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
new String[] {"item 1","item 2"},
new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter(simpleAdapter);
}
}
----------------아래는 main_activity.xml의 내용입니다.----------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:visibility="gone"/>
</RelativeLayout>
+추가) 로그
02-10 22:00:57.942: E/AndroidRuntime(1052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dongdonge.achievement/com.dongdonge.achievement.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=20; index=20
이러는데... 검색해보니 자기 사물함뿐만 아니라 남의 사물함까지 사용해서(비유) 그렇다는군요.
그런데 어디를 잘못한건지 모르겠어요 ㅠㅠ 도와주세요~~




ava.lang.ArrayIndexOutOfBoundsException: length=20; index=20
이 에러는요 [ array = 배열, index = 순서 , 가 Out = 벗어남, of = ~의, Bounds = 범위, Exception = 예외 ] 입니다.
뒤의 length = 20 은 array 의 bounds 가 20 이란 말이고
그 뒤의 index = 20 은 순서가 20이란 말입니다.
종합해보면,
배열 순서가 범위를 벗어난 예외가 발생했습니다. 범위는 = 20 , 순서 = 20
java에서 배열은 0 부터 시작하기 때문에 갯수가 20개라면 index는 0~19 입니다.