使用ListFragment在Android FragmentActivity中找不到ID的视图

No View Found For id in Android FragmentActivity with ListFragment

我正在尝试创建自定义ListView。 测试代码时,出现以下错误

1
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}

除了阅读有关Fragments,ListFragments和ListViews的Android文档外,在发布此问题之前,我还引用了以下链接。

链接1:FragmentActivity onCreateView

链接2:在Android上使用ListFragment时出现问题

链接3:Android片段找不到ID的视图?

链接4:Android ListFragment令人困惑

链接5:如何从FragmentActivity更新ListFragment中的ListView?

链接6:使用自定义视图填充ListFragments?

我有扩展了FragmentActivity的TeamActivity类和扩展了ListFragment并实现LoaderManager.LoaderCallbacks的TeamListFragment。 请参见下面的代码:

TeamActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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);
    }

}

除了上面的代码,我还有以下自定义布局。

team.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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>


在Activity的onCreate()方法中,使用方法setContentView(R.layout.activity)并定义一个布局文件,该文件在LinearLayout内部包含一个FrameLayout

现在,使用刚刚添加的FrameLayout的ID代替onCreate()中使用的R.id.android_teamList调用add()方法,并在team.xml布局文件中用android:id="@android:id/list"替换android:id="@+id/android:teamList"

顺便说一句,如果需要的话,可以使用android.R.id.list访问ListView。


R.id.text1应该在R.layout.team中定义。

检查SimpleCursorAdapter文档:

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"