Boardy
Boardy

Reputation: 36223

Using screen fragments on android

I am working on android app and am trying to get fragments working but I've run into a problem.

When the app launches it loads an activity which is supposed to house the two fragments. Fragment1 contains a list of stored logins and fragment 2 will show the details associated with that login.

The list fragment is supposed to retrieve data from the SQLite database and display on the screen and once the item is clicked, it loads the second fragment, but I am having problem getting the first stage working.

In my main activity class I have the following.

public class PasswordListMain extends Activity {

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.password_management);
    }

}

The password_management XML file contains the following

<?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:orientation="vertical" >

    <fragment
        android:id="@+id/passwordList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.BoardiesITSolutions.PasswordManager.PasswordListFrag">
    </fragment>
</LinearLayout>

It just contains the fragment as this is the portrait screen, I thought I'd get this bit working before getting it working side by side.

The PasswordListFrag contains the following

public class PasswordListFrag extends ListFragment{

    Common common; 
    com.BoardiesITSolutions.logic.ManagePasswordList managePasswordList;

    ArrayList<String> savedPassword;
    TextView txtNoRecords;
    ListView myListView;
    ArrayAdapter<Spanned> passwordArrayAdapter;
    AdView adView;

    @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflator.inflate(R.layout.password_list, container, false);

        myListView = getListView();
        common = new Common(getActivity().getApplicationContext());

        //AdView adView = (AdView)findViewById(R.id.adView);
        common.requestAdvert(adView);

        managePasswordList = new ManagePasswordList(getActivity().getApplicationContext());
        //txtNoRecords = (TextView)findViewById(R.id.password_noRecords);
        populateListArray();

        common.showToastMessage("Adapter updated", Toast.LENGTH_LONG);

        //myListView.setOnItemClickListener(mListView);

        return view;
    }

    private void populateListArray()
    {

        ArrayList<Spanned> passwords = managePasswordList.getPasswordList();
        if (passwords != null && passwords.size() > 0)
        {
            passwordArrayAdapter = new ArrayAdapter<Spanned>(getActivity().getApplicationContext(), 
                    android.R.layout.simple_list_item_1, passwords);
            setListAdapter(passwordArrayAdapter);
            passwordArrayAdapter.setNotifyOnChange(true);
            myListView.setTextFilterEnabled(true);
            txtNoRecords.setVisibility(View.GONE);
        }
        else
        {
            txtNoRecords.setVisibility(View.VISIBLE);
        }
    }
}

Below is the XML for the list fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView android:id="@+id/password_noRecords"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="There are currently\nno saved logins"
        android:textSize="20dp"
        android:textStyle="bold|italic"/>
    <ListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/adView">
    </ListView>
        <com.google.ads.AdView android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ads:adUnitId="5555555"
            ads:adSize="BANNER"
            android:layout_alignParentBottom="true">
        </com.google.ads.AdView>
</RelativeLayout>

When the app loads it instantly crashes when it tries to load this screen.

The error is

java.lang.RuntimeException: Unable to start activity ComponentInfo PasswordListMain: android.view.InflatException: Binary XML file line #7 Error inflating class fragment

I have no idea what's causing this or how to fix the problem.

Thanks for any help you can provide.

Upvotes: 1

Views: 221

Answers (1)

JRaymond
JRaymond

Reputation: 11782

My best guess for the issue is this line (but admittedly I'm unsure):

myListView = getListView();

You're calling it before the fragment has a view - and I don't think that will work too well. I would recommend doing your initialization work of the views involved in onActivityCreated() instead of in onCreateView() and the other components (like your data store thing and Common) in onCreate()

Upvotes: 2

Related Questions