如何在不覆盖onCreateView的情况下调用ListFragment中的findViewById()?

时间:2015-12-08 17:42:08

标签: android android-fragments android-listfragment

我不想覆盖onCreateView,因为没有必要。使用ListFragment我正在做的是获取数据数组,将它放在ArrayAdapter中并调用setListAdapter(arrayGoesHere)然后我有我填充的ListFragment。

我在onActivityCreated()中调用findViewById,因为它是查找和存储对视图的引用的推荐位置。如你所知,这是在Android框架中的onCreateView()之后调用的。

我无法执行viewReturnedFromOnCreateView.findViewById,因为我没有使用onCreateView。

getActivity()。findViewById不起作用,因为我实际上不确定原因。

getListView()。findViewById不起作用(因为我试图访问的元素不是getListView()的子元素。)

编辑1:getView()也没有工作,我忘了提到我正在使用支持库android.support.v4.app.ListFragment。不确定是否重要

这是我的代码:

public class SomeFragment extends ListFragment {

private int someButtonId;
private Button someButton;

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

    String[] someList = getResources().getStringArray(R.array.someData);

    ArrayAdapter<String> someAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, someList); 
    setListAdapter(someAdapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    context = getActivity().getApplicationContext();
    someButtonId = context.getResources().getIdentifier("someButton", "id", context.getPackageName());
    someButton = (Button) getActivity().findViewById(someButtonId);
    Log.i("hello", someButton.toString()); //Null pointer exception
}
}

0 个答案:

没有答案
相关问题