2012-08-23 4 views
0

그래서 같은 레이아웃의 단추 아래에 ListView가 채워지려고합니다. 기본적으로 ListView 옵션이 선택되면 버튼은 선택한 각 옵션에 다르게 응답합니다. 문제는 Force가 닫히고 ListView를 채우는 데 문제가있는 것 같습니다.ListView 아래에있는 버튼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background" 
    > 
<Button 
    android:background="@drawable/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="15dp" 
    android:layout_gravity="center" 
    android:id="@+id/button"/> 

    <ListView 
    android:listSelector="#990000" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="10dp" 
    android:id="@+id/listview" 
    /> 
</LinearLayout> 

코드 :

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    lv = (ListView) findViewById(R.id.listview); 

    ArrayList<String> list = new ArrayList<String>(); 
    soundsList.add("one"); 
soundsList.add("two"); 
soundsList.add("three"); 



    ArrayAdapter<String> arrayAdapter =  
      new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list); 
      lv.setAdapter(arrayAdapter); 

로그 캣 판독이있다 :

08-23 10:21:23.735: ERROR/AndroidRuntime(588): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 

나는 (... ArrayAdapter와의 android.R.id.list을 말한다 정확히 시도 . 나는 ListActivity를 확장하는 것을 포함하여 많은 다른 것들을 시도했지만, 내가 시도한 것에 관계없이 비슷한 Logcat 응답을 얻는 것처럼 보인다.

내가 성취하려는 일을 해내는 더 좋은 방법이 있습니까? 같은 main.xml에있는 ListView 위의 버튼을 원합니다.

미리 감사드립니다.

편집 :

이 코드도 사용해 보았습니다. 현재 댓글을 달았지만 원래 시도한 내용이었습니다.

setListAdapter(new ArrayAdapter<String>(this, android.R.id.list,LIST)); 

    ListView listView = getListView(); 
    listView.setTextFilterEnabled(true); 

LIST는 문자열 배열입니다. 위의 코드에서 Logcat의 출력은 이전과 동일합니다.

+0

LogCat 오류는 자체 수정 방법을 설명하고 Nick은보다 자세한 답변을 제공했습니다. 'id '를 Nick의 코드로 바꾸는 것이 좋습니다. – Sam

답변

1

ListActivity 또는 ListFragment를 구현하려고 시도하는 것처럼 들리므로 특정 이름 (예 : 오류)의 listview 인스턴스를 찾으려고합니다. 에서 목록보기의 ID를 변경해보십시오 :

android:id="@+id/listview" 

에 :

android:id="@android:id/list" 

난 당신이 이미 시도했지만 코드를 부여했다 것을 알고는, 그것 여전히 잘못된 것 같습니다. 일단 변경하면 새로운 오류 메시지를 게시 할 수 있습니까? 나는 당신의 설명에 따라 그것이 비슷한 메시지가 될 것이라는 것을 깨달았습니다. 그러나 그 차이가 경미하더라도 여전히 중요 할 것입니다.

편집 :

난 그냥 눈치 또 다른 한가지 - 당신은 참으로 ListActivity 또는 FragmentActivity를 사용하는 경우, getListView() instead of findViewById를을()`를 호출하여 ListView에에 심판을받을.

+0

응답 해 주셔서 감사합니다. Yea 나는 이것을 하나의 샷으로 주었고 나는 그것을 다시 시도했다. 그것을 변경 한 다음 lv findViewById를 (android.R.id.list)로 변경하고 컴파일하여 동일한 오류가 발생했습니다. 그런 다음 ArrayAdapters를 (android.R.id.list)로 변경했습니다. 다음은 Logcat에 대한 출력입니다. – steven

+0

08-23 10 : 51 : 05.215 : ERROR/AndroidRuntime (651) : 원인 : java.lang.RuntimeException : 귀하의 콘텐츠에는 id 속성이 'android.R.id'인 ListView가 있어야합니다. .list ' – steven

+0

ListActivity 또는 ListFragment를 사용하고 있는지 확인할 수 있습니까? 그것은 확실히 그것 같이 보인다. – Nick

관련 문제