0

findViewById(R.id.team_detail_container)找不到视图。我的问题是 xml 还是我构建 FragmentActivity 的方式?我该如何解决这个问题?

为了在片段活动的构造函数中支持片段的自定义列表,我已替换

setContentView(R.layout.activity_team_list);

frag=(TeamListFragment)getSupportFragmentManager().findFragmentById(android.R.id.content);
        if (frag==null) {
            frag=new TeamListFragment();
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();

所以我的课现在看起来像这样

public class TeamListActivity extends SherlockFragmentActivity implements
        TeamListFragment.Callbacks {

    private boolean mTwoPane;
    private TeamListFragment frag=null;

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

        frag=(TeamListFragment)getSupportFragmentManager().findFragmentById(android.R.id.content);
        if (frag==null) {
            frag=new TeamListFragment();
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
        }
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (findViewById(R.id.team_detail_container) != null) {

            Log.i("@@@@", "Team Detail Container has been found! Yaay!");
            mTwoPane = true;
            ((TeamListFragment) getSupportFragmentManager().findFragmentById(
                    R.id.team_list)).setActivateOnItemClick(true);
        }   
    }

尽管使用的if (findViewById(R.id.team_detail_container) != null) {xml 文件中存在 team_detail_container,但条件从未满足

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/team_list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal"
        android:showDividers="middle"
        tools:context=".TeamListActivity" >

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/team_list"
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:paddingLeft="8dp"
                android:paddingRight="8dp">

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />

            <TextView android:id="@id/android:empty"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:text="No data"/>
         </LinearLayout>        
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="?android:attr/listDivider"
        /> 

        <FrameLayout
            android:id="@+id/team_detail_container"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />

    </LinearLayout>

我知道这个特定的布局 xml 正在被使用,因为我对 xml 文件所做的更改会反映在应用程序中。I also know that the condition is never met as I never get the log message Log.i("@@@@", "Team Detail Container has been found! Yaay!");and the behaviour get is that when an item is selected the list view is replaced with the detail fragments instead of the detail fragments being displayed next to the list view because the不满足项目选择的条件

@Override
public void onItemSelected(int id) {
    if (mTwoPane) {
                // mTwoPane is never set! Why?

        Bundle arguments = new Bundle();
        arguments.putInt(TeamDetailFragment.ARG_ITEM_ID, id);
        TeamDetailFragment fragment = new TeamDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.team_detail_container, fragment).commit();

    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, TeamDetailSwipeActivity.class);
        detailIntent.putExtra(TeamDetailFragment.ARG_ITEM_ID, id);
        startActivity(detailIntent);
    }
}
4

2 回答 2

1

我认为问题在于您正在从 Activity 中寻找 Fragment ,但是由于您没有使用setContentView,因此 Activity 实际上没有要查看的 View 。尝试从 Fragment 获取视图,而不是使用该getView()方法。

于 2013-01-24T15:09:34.630 回答
1

提交事务后,片段不会立即添加到您的布局树中。你必须等到onViewCreated片段被调用。您可能可以在onStartActivity 的 -Method 中访问视图,但最好将该逻辑保留在片段本身中。Activity 不应该关心片段中包含哪些视图。

于 2013-01-24T15:31:22.587 回答