unknown unkonwn
unknown unkonwn

Reputation: 81

ListView setAdapter() doesn't work

I am using a ListView widget in a Fragment class. Not in ListFragment, because I am using other widgets in the same fragment. Therefore using the ListFragment class will cause the ListView to take the entire screen. However, using an instance of the ListView class, "setAdapter()" causes an app crash. Here is my code

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] systems = {"IOS","Android","Windows","Mac","Linux"};
        ListView lv = (ListView) getActivity().findViewById(R.id.mainList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, systems);
        lv.setAdapter(adapter);
}

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

When I wrap "lv.setAdapter(adapter);" in a try-catch block the app doesn't crash. Which is why I think that it's the problem.

Here is the Fragment's layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/mainList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/darkbg"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/butEdit"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="@drawable/button_style"
            android:text="@string/delete_button"
            android:textColor="#FFFFFF" />

        <Button
            android:id="@+id/butOpen"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="@drawable/button_style"
            android:text="@string/open_button"
            android:textColor="#FFFFFF" />
    </LinearLayout>

</LinearLayout>

And here is the activity_main.xml code:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainActivityContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.my.app.MainActivity" >

    <fragment
        android:id="@+id/listFragment"
        android:name="org.my.app.fragments.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        tools:layouts="@layout/fragment_list" />

</FrameLayout>

Upvotes: 0

Views: 2727

Answers (1)

Simas
Simas

Reputation: 44118

onCreate is called before onCreateView, therefore the fragments layout is not yet inflated there.

You need to move your code that deals with the fragment's layout to onCreateView where all the inflation happens:

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

    String[] systems = {"IOS","Android","Windows","Mac","Linux"};

    ListView lv = (ListView) rootView.findViewById(R.id.mainList);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, systems);
    lv.setAdapter(adapter);

    return rootView;
}

Upvotes: 1

Related Questions