0

我试图在我的列表视图上放置一个“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

4

1 回答 1

0

我能够回答它。查看文档后,我发现设置列表视图的代码必须在 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;
    }
        }
    });
}
于 2013-10-11T09:34:15.170 回答