8

I have a problem that seems to be quite common on the internet : I have created an activity with just a fragment. This is the generated code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
            ListView lv = (ListView) findViewById(android.R.id.list);
        }
    }

After or inside the condition, I can't get any object with findViewById: it is always null. Here is the rest of the generated code :

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_customer,
                container, false);
        return rootView;
    }

activity_customer.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.axacs.marc.CustomerActivity"
    tools:ignore="MergeRootFrame" />

the listview is actually in fragment_customer.xml:

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/new_folder_button"
    android:text="" />

Does anyone know what is missing ?

Charline
  • 283
  • 1
  • 2
  • 13
  • 1
    Listview belongs to `fragment_customer.xml`.post the same – Raghunandan Jul 24 '14 at 09:48
  • Do clean build your project. Show your activity_customer.xml – Shraddha Shravagi Jul 24 '14 at 09:48
  • 1
    ListView lv = (ListView) findViewById(android.R.id.list); move this line on fragment onCreateView() – Haresh Chhelana Jul 24 '14 at 09:51
  • 4
    `View rootView = inflater.inflate(R.layout.fragment_customer,container, false); ListView lv = (ListView) rootView.findViewById(android.R.id.list); return rootView;` – Raghunandan Jul 24 '14 at 09:52
  • Do you get the context with `getActivity().getApplicationContext()` then or is it something else? – Charline Jul 24 '14 at 09:56
  • 1
    Thank you very much. I'm not used to fragments but it seems my problem was very simple. I've had the same problem with a viewpager, but your solution didn't work in that case. I will post another question if it keeps bothering me. Thank you again! – Charline Jul 24 '14 at 10:03
  • @Raghunandan: Thanks. I was doing getActivity.findViewbyId(), but that also returned null. Finally tried inflater.inflate.findViewById as your solution suggests and it worked. – ARK Oct 25 '15 at 18:53

1 Answers1

24

use following after moving this line to onCreateView() if you want to use your listview in your fragment

ListView lv = (ListView)rootview.findViewById(android.R.id.list);
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
  • `cannot resolve "rootview" symbol` :( – dialex Mar 03 '16 at 15:28
  • 1
    @dialex rootview is not a predefined symbol.Its the name given to the view that is going to be inflated in the fragment.It can have any name. – Anirudh Sharma Mar 04 '16 at 03:17
  • 1
    @dialex your function->onCreateView should looks like -> Bundle savedInstanceState) { View rootview=null; rootview=inflater.inflate(R.layout.fragment_web_view, container, false); ListView lv = (ListView)rootview.findViewById(android.R.id.list); return rootview; – sms247 Mar 24 '16 at 06:51