2015-01-21 6 views
0

나는 활동중인 애플리케이션을 활동에서 단편으로 이동시키려는 Android 프로그래밍 초보자입니다. IllegalStateException이 (여기 fullscreen 함께ListFragment에서 getListView(). setChoiceMode()를 호출하면 IllegalStateException이 발생합니다.

public class ScanningFragment extends ListFragment { 
    private ScanningListener mListener; 

    static class Beacon { 
     public String address; 
     public int rssi; 
    } 

    static class ViewHolder { 
     public ImageView image1; 
     public CheckedTextView text1; 
    } 

    private ArrayList<Beacon> mBeacons = new ArrayList<Beacon>(); 
    private ArrayAdapter<Beacon> mAdapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.activity_scanning, container, false); 

     mAdapter = new ArrayAdapter<Beacon>(getActivity(), 
       R.layout.rowlayout, 
       R.id.text1, 
       mBeacons) { 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       ViewHolder holder; 

       if (convertView == null) { 
        LayoutInflater inflater = getActivity().getLayoutInflater(); 
        convertView = inflater.inflate(R.layout.rowlayout, null); 
        holder = new ViewHolder(); 
        holder.image1 = (ImageView) convertView.findViewById(R.id.image1); 
        holder.text1 = (CheckedTextView) convertView.findViewById(R.id.text1); 
        convertView.setTag(holder); 
       } else { 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       Beacon beacon = (Beacon) getItem(position); 
       holder.text1.setText(beacon.address); 
       // TODO pass selected address here 
       holder.text1.setChecked(beacon.address.equalsIgnoreCase(CommonConstants.ADDRESS_DEFAULT)); 
       int res = CommonConstants.rssi2res(beacon.rssi); 
       holder.image1.setImageResource(res);     
       return convertView; 
      } 
     };  

     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // Exception! 
     setListAdapter(mAdapter); 

     return view; 
    } 

불행하게도 라인

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

폭탄 : 나는 다음과 같은 코드를 삽입 한 (인근 블루투스 장치 목록을 표시해야합니다)를 ScanningFragment에서

) :

screenshot

trace

알고 제발 누군가가, 내가 잘못 여기서 뭐하고 있나요 :

그리고 여기 트레이스 (여기 fullscreen, 미안 해요 이클립스 디버그 창에서 텍스트를 복사하는 방법을 모른다)인가?

답변

1

onCreateView() 대신 해당 줄을 onViewCreated()으로 옮깁니다. onCreateView()ListView이 아직 생성되지 않았으므로 검색하려고하면 IllegalStateException이 표시됩니다.

또한 activity_scanning.xml에는 ListViewandroid:id="@android:id/list"이 포함되어야합니다.

+0

감사합니다 +1. "inflate"콜 이후에 한 줄 또는 모든 것을 움직여야합니까? –

+0

아마 하나만 이동해야하지만, 일반적으로 모든 뷰 초기화를 onViewCreated에서 수행하고 onCreateView에서 인플레이션 만 수행합니다. – kcoppock

관련 문제