0

I don't want to override onCreateView because there is no need. With a ListFragment all I am doing is taking an array of data, putting it in an ArrayAdapter and calling setListAdapter(arrayGoesHere) and then I have my populated ListFragment.

I am calling findViewById inside onActivityCreated() as it is the recommended place to find and store references to your views. And as you know, this is called after onCreateView() in the Android framework.

I can't do viewReturnedFromOnCreateView.findViewById because I'm not using onCreateView.

getActivity().findViewById doesnt work, because I'm actually not sure why.

getListView().findViewById doesnt work (because the element im trying to access is not a child of getListView()).

Edit 1: getView() didn't work either, I forgot to mention I am using the support library, android.support.v4.app.ListFragment. not sure if that matters

This is my code:

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
}
}
Lv99Zubat
  • 853
  • 2
  • 10
  • 27
  • [`getView()`](http://developer.android.com/reference/android/app/Fragment.html#getView%28%29) – Kevin Krumwiede Dec 08 '15 at 17:44
  • r u looking for getView()? method and check if necessary to call getApplicationContext on getActivity(). Maybe getActivity should fit enought – Tomas Cejka Dec 08 '15 at 17:46
  • getView() didn't work either, I forgot to mention I am using the support library, android.support.v4.app.ListFragment. not sure if that matters. i will edit that in post – Lv99Zubat Dec 08 '15 at 17:48
  • Why are you not using onCreateView? Is the view you are trying to find in this fragment, or the holding activity? – OneCricketeer Dec 08 '15 at 18:16
  • @cricket_007 with onCreateView, don't I HAVE to return the view which I inflate the fragment with? and I'm not inflating the fragment with a layout because I am using a ListFragment with an ArrayAdapter. So I don't have a view to return. – Lv99Zubat Dec 08 '15 at 18:23
  • 1
    The default implementation does have a view to return though, so I think you can do something like `View v = super.onCreateView(); v.findViewById(); return v;` – OneCricketeer Dec 08 '15 at 18:25
  • @cricket_007 hmm, that was a great idea and I expected that to work. But I think there must be something else wrong with my code. I simply can't find that button id. Not sure what's going on. – Lv99Zubat Dec 08 '15 at 18:41
  • @cricket_007 this has got me stumped. I tried just doing v.findViewById(R.id.someButton) and if I ctrl click R.id.someButton, it takes me right to the xml file with the button. Could it be because the button is not visible on the page? – Lv99Zubat Dec 08 '15 at 19:13
  • @cricket_007 another thought, Im pretty sure "whatever I am trying to find" needs to be a child of the view that I use to call findViewById. So I tried view.getRootView().findViewById(R.id.someButton) and I thought for sure that was gonna work but it didn't... D: – Lv99Zubat Dec 08 '15 at 19:41
  • Hmm. Not sure what to tell you, really. I've got 2 sources [here](http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment) and [here](http://stackoverflow.com/questions/12324623/findviewbyid-not-recognised-in-listfragment?rq=1) that might be able to help you out. Otherwise, I would like to know in which XML this button is defined. You extend ListFragment, so that will use the default XML, which does not have a button, so the only other place you would be trying to get it is from the parent activity. – OneCricketeer Dec 08 '15 at 22:37

0 Answers0