문제

I've got a Fragment containing it's own layout. This layout has a ListView. Now I've tried to add an adapter for this ListView like this:

public static class LatestFragment extends Fragment {
    ListView listView;
    String[] values = {
            "Test", "Test2"
    };

    public LatestFragment() {

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

        View rootView = inflater.inflate(R.layout.fragment_latest, container, false);
        listView = (ListView) rootView.findViewById(R.id.fragment_latest_listview1);
        listView.setAdapter(new CustomListAdapter(getActivity(), values));
        return rootView;
    }
}

But I keep getting an error at

new CustomListAdapter(getActivity(), values)

Eclipse tells me following:

No enclosing instance of type MainActivity is accessible. Must qualify the
allocation with an enclosing instance of type MainActivity (e.g. x.new A() 
where x is an instance of MainActivity).

Why can't I set up an adapter for my ListView?

도움이 되었습니까?

해결책

My guess is that CustomListAdapter is not defined as a static inner class, and therefore it cannot be used from another static inner class.

Also, please get rid of the constructor, or at least chain to the superclass constructor.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top