4

I'm attempting to create a custom ListView. When I test my code, I receive the following error

03-09 19:21:10.425: E/AndroidRuntime(379): java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.anomaly.punchlist/com.anomaly.punchlist.TeamActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f08000c for fragment TeamListFragment{4067a358 #0 id=0x7f08000c}

I have referenced the following links prior to posting this question in addition to reading the Android Docs on Fragments, ListFragments, and ListViews.

Link 1: FragmentActivity onCreateView
Link 2: Problem using ListFragment on Android
Link 3: Android Fragment no view found for ID?
Link 4: Android ListFragment is to confusing
Link 5: How update ListView in ListFragment from FragmentActivity?
Link 6: Populate ListFragments with a custom view?

I have class TeamActivity which extends FragmentActivity and TeamListFragment which extends ListFragment and implements LoaderManager.LoaderCallbacks. Please see code below:

TeamActivity.java

package com.anomaly.punchlist;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class TeamActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction();
    TeamListFragment teamListFragment = new TeamListFragment();
    fragmentTransaction.add(R.id.android_teamList, teamListFragment);
    fragmentTransaction.commit();       
   }

 }

TeamListFragment.java

package com.anomaly.punchlist;

    import android.database.Cursor;
    import android.os.Bundle;
    import android.support.v4.app.ListFragment;
    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.CursorLoader;
    import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.anomaly.punchlist.contentprovider.basecolumns.TeamBaseColumns.Team;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.logger.LoggerFactory;

public class TeamListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static Logger logger = LoggerFactory.getLogger(TeamListFragment.class);
    private ListView listView;
    private SimpleCursorAdapter adapter;
    private static final int TEAM_LOADER_ACTIVITY = 0x01;
    private static final String[] PROJECTION = new String[] { "project_manager_id", "contact_id", "project_id" };
    private static String SELECTION = "contact_id = ? AND project_id = ?";

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            int[] uiBindTo = { R.id.text1 };
            getLoaderManager().initLoader(TEAM_LOADER_ACTIVITY, null, this);
            adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.team, null, null, uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        } catch (Exception e) {
            logger.error(e.getMessage());
            new RuntimeException("RuntimeException occurred in onCreate() method of TeamActivity." + e);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle bundle) {

        String contactId = new String();
        String projectId = new String();
        CursorLoader cursorLoader = null;

        if (bundle != null) {
            contactId = bundle.getString("contact_id");
            projectId = bundle.getString("project_id");

        }
        String[] selectionArgs = { contactId + ", " + projectId };
        cursorLoader = new CursorLoader(getActivity(), Team.CONTENT_URI, PROJECTION, SELECTION, null, null);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor);
        setListShown(true);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursor) {
        adapter.swapCursor(null);
    }

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        listView = getListView();
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setListAdapter(adapter);
        setListShown(true);
    }

}

In addition to the code above, I have the following custom layout.

team.xml

<?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"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >


    <ListView
        android:id="@+id/android:teamList"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/grey2"
        android:divider="@color/blue2"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:text="No data" />
</LinearLayout>
Community
  • 1
  • 1
dionysus
  • 439
  • 3
  • 12
  • 26
  • 1
    What has this id 0x7f08000c? You can find this answer in you gen//R.java file. – Hartok Mar 10 '13 at 02:32
  • @ Hartok: It is referenced in an layout xml call team_row.xml which has a TextView the an android id that equals text1. 0x7f08000c = text1. – dionysus Mar 10 '13 at 02:36
  • @Hartok: Thanks again for the help. However, I put the referenced id inside of my team.xml file and now I receive the following Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f08000c for fragment TeamListFragment{4065f128 #0 id=0x7f08000c} – dionysus Mar 10 '13 at 03:32
  • @Hartok BTW the referenced id is a TextView inside of the team.xml file. – dionysus Mar 10 '13 at 03:47
  • It's exactly the same error as before? – Hartok Mar 11 '13 at 04:39
  • @Hartok I've resolved this particular error. Your advice is much appreciated. – dionysus Mar 11 '13 at 19:04
  • 1
    You should not modify R.java –  Mar 15 '15 at 19:28

2 Answers2

6

In onCreate() method of your Activity use the method setContentView(R.layout.activity) and also define a layout file, which contains a FrameLayout inside LinearLayout.

Now invoke add() method with the id of FrameLayout you just added instead of R.id.android_teamList that you are using in your onCreate() and also replace android:id="@+id/android:teamList" with android:id="@android:id/list" in your team.xml layout file.

By the way, in case you must, you can access your ListView with android.R.id.list.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • Thanks for the help. However, how do I retrieve this in fragmentTransaction.add(R.id.listView1, teamListFragment); ? – dionysus Mar 10 '13 at 02:31
  • 1
    Wait a sec... something else is wrong too... updating my answer. – M-Wajeeh Mar 10 '13 at 02:34
  • Why do I need to create another layout file? Shouldn't the team.xml layout file be sufficient? – dionysus Mar 10 '13 at 03:34
  • This is the documentation I have read [ListFragments](http://developer.android.com/reference/android/app/ListFragment.html) and [Fragments](http://developer.android.com/guide/components/fragments.html). What docs have you read that I have not? Please provide. – dionysus Mar 10 '13 at 21:30
  • 2
    Please read http://developer.android.com/guide/components/fragments.html#Example Focus on fragment_layout.xml, See how we need a `FrameLayout` to place `Fragment`s dynamically in an `Activity`, and then you just call `ft.replace(R.id.details, details);` – M-Wajeeh Mar 11 '13 at 07:41
1

R.id.text1 should be defined in R.layout.team.

Check the SimpleCursorAdapter doc:

resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"

Hartok
  • 2,147
  • 20
  • 37
  • Thanks for the all help. But there is one more additional part to getting all of this to work due to a bug in Compatibility Library v4 ListFragment uses internal id for 'empty' TextView. See [Compatibility Bug](https://code.google.com/p/android/issues/detail?id=21742) and [Solution](http://stackoverflow.com/questions/10608624/list-fragment-does-not-accept-my-layout) for more info. – dionysus Mar 13 '13 at 05:06