方法findViewById(int)也未定义适配器的类型和控制器

时间:2013-11-17 20:15:40

标签: java android listview adapter

我收到此错误:方法findViewById(int)未定义类型。

我有一个片段:

public class EventFragment extends Fragment  {


    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.event, 
                container, false);

     // Check for an open session
        Session session = Session.getActiveSession();
        if (session != null && session.isOpened()) {
            // Get the user's data
            makeFQLRequest();

        }
        return view;
    }

这是我的方法makeFQLRequest();

private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";


        //Create a JSON parser Instance ----- Used JSON parser from Android
        JSONParser jParser=new JSONParser();

        //Getting JSON string from URL ------ Used JSON Array from Android
        JSONArray json=jParser.getJSONFromUrl(url);

        List<WhateverObject> yourData = new ArrayList<WhateverObject>();

        try {
            for(int i=0;i<json.length();i++)
            {
                JSONObject c=json.getJSONObject(i);// Used JSON Object from Android

                //Storing each Json in a string variable
                int AGE=c.getInt("age");
                String NAME=c.getString("name");
                String CITY=c.getString("city");
                String GENDER=c.getString("Gender");
                String BIRTHDATE=c.getString("birthdate");


                yourData.add(new WhateverObject(NAME, CITY, GENDER, BIRTHDATE));

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
if(yourData != null){

        ListView yourListView = (ListView) findViewById(R.id.itemListView);

        ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);

        yourListView.setAdapter(customAdapter);

    }

现在我甚至无法测试我的代码,因为它表示方法findViewById(int)未定义类型,并且适配器的构造函数未定义。任何人都可以帮助我做什么我现在被困了2个多小时......

2 个答案:

答案 0 :(得分:0)

使用getView()。这将返回片段的根视图,您可以调用它来调用findViewById()。

YourView ref= (YourView ) getView().findViewById(R.id.foo);

答案 1 :(得分:0)

在片段中使用getActivity()来检索如下视图:

ListView yourListView = (ListView) getActivity().findViewById(R.id.itemListView);