在片段上加载listview时未创建内容视图

时间:2013-10-11 09:02:52

标签: android listview android-fragments

我正在尝试在列表视图中放置一个“Onclicklistener”,但是logcat说“内容视图未创建”

这是我的代码:

ListPlacesFragment.java

   public class ListPlacesFragment extends ListFragment {

   String[] countries = new String[] {
        "Home", "Places", "Maps", "Reviews", "Facebook"
   };    

   int[] flags = new int[]{
        R.drawable.homeg,R.drawable.placesg,R.drawable.mapg,R.drawable.reviewg,R.drawable.fbg
   };

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

    View rootView = inflater.inflate(R.layout.activity_list_places_fragment, container, false);


    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

    for(int i =0; i <5;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", countries[i]);
        hm.put("flag", Integer.toString(flags[i]) );            
        aList.add(hm);        
    }

    // Keys used in Hashmap
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.imagePreview,R.id.textDesc};        

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_item, from, to);       

    setListAdapter(adapter);

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
    switch(position) {
            case 0: 
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/")));

                break;
            case 1:
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/")));

                break;
    }
        }
    });

    return rootView;
   }



  }

我之前已经阅读了一些关于此的帖子,但之前的成员试图实现的与我的不同。另一个是宣布观点后宣布的。请告诉我有什么问题。这是我的Logcat。谢谢!

http://shrib.com/content

1 个答案:

答案 0 :(得分:0)

我能够回答它。在查看docs后,我发现设置列表视图的代码必须在onActivityCreated中。

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

   List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

    for(int i =0; i <5;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", countries[i]);
        hm.put("flag", Integer.toString(flags[i]) );            
        aList.add(hm);        
    }

    // Keys used in Hashmap
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.imagePreview,R.id.textDesc};        

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_item, from, to);       

    setListAdapter(adapter);

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
    switch(position) {
            case 0: 
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/")));

                break;
            case 1:
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/")));

                break;
    }
        }
    });
}