2012-04-15 3 views
0

ListView의 내용을 TextViews의 배열로 설정하려고합니다.ListView 어댑터 설정 : null 포인터 예외

ListView lvSuggestions = (ListView)findViewById(R.id.lvSuggestions); 

ArrayAdapter<TextView> arrayAdapter = new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1); 
lvSuggestions.setAdapter(arrayAdapter); 

하지만을 : 여기 how do I add items to listview dynamically in android

내가 현재 가지고 무엇을 : 나는 내가 무엇을 여기에서 제안하고있는 거라고 생각에도 불구하고, 널 (null) 포인터 예외를 얻을 내가 현재 가지고와

어댑터를 설정할 때 예외를 얻으십시오.

TextViews로 ArrayList를 채운 다음 전체 ArrayList로 어댑터를 만드는 것이 시도했지만 어댑터를 설정할 때 동일한 Null 포인터 예외가 발생합니다. 반환 할 수 있습니다 도움이

답변

0

미국에 대한 링크를 참조하십시오. 사용자 지정 어댑터의 ge.

LINK1

LINK2

LINK3

LINK4

LINK5

LINK6

+0

고마워요, 이것에 대해 살펴 보겠습니다. –

0

(ListView)findViewById(R.id.lvSuggestions); 

에 대한

덕분에 nullnull 반환 findViewById 경우 (어떤 항목이 발견되지 않는) 또는 반환 된 객체 유형 ListView이 아닌.

로 보호 :

if (lvSuggestions != null) { 
    ArrayAdapter<TextView> arrayAdapter = new ArrayAdapter<TextView>(this, 
     android.R.layout.simple_list_item_1); 
    lvSuggestions.setAdapter(arrayAdapter); 
} 
+0

당신이 옳을 때 예외를 반환하는 것은 그 라인이 아닙니다. 그것은 : lvSuggestions.setAdapter (arrayAdapter); –

+0

예,'lvSuggestions.'lvSuggestions'가'null' 인 경우 setAdapter()'는 NullPointerException을 던집니다. – Attila

+0

아, 이해합니다. 그러나 그것은 null이 아닙니다. 디버깅에서 볼 수 있습니다. –

1
ArrayAdapter<TextView> arrayAdapter = new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1); 

변화

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1); 

와 당신이 ArrayAdapter와에 표시 할 모든 문자열을 추가 아래에 위의 행으로

arrayAdapter.add("string1"); 
arrayAdapter.add("string2"); 
arrayAdapter.add("string3");//so on 
아래
+0

하지만 채우기를 원하지 않습니다. 문자열로. TextViews로 채울 수 있습니까? –

+0

심지어 textview에서 문자열로 settext해야합니다 –

+0

그래, 문자열로 Textview 텍스트를 설정해야 할 것입니다. 하지만 TextViews의 ID를 설정할 수 있기를 원하기 때문에 내 ListView에 TextViews 배열이 포함되기를 원합니다. TextView 형식의 ArrayList로 ListView를 채울 수 있습니까? –

0

아마도 setAdapter 뒤에 setContentView를 놓을 수 있습니다.

그것은 그렇게해야합니다 :

된 setContentView (XXX);

// ....

는, setAdapter (XXX)

가 도움이 희망!