2013-01-24 29 views
0

findViewById(R.id.team_detail_container)無法找到視圖。是我的問題與XML或以我構建FragmentActivity的方式?我該如何解決這個問題?Android片段活動佈局未在多窗格佈局中使用findViewById查找視圖

爲了支持自定義列表的片段在我的片段活動的構造函數中我已經取代

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) {條件永遠都無法儘管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文件,反映在應用程序中的XML文件存在的事實得到滿足。 我也知道條件從來沒有滿足,因爲我從來沒有得到日誌消息Log.i("@@@@", "Team Detail Container has been found! Yaay!");和行爲得到的是,當一個項目被選中時,列表視圖被替換爲詳細片段而不是顯示在列表視圖旁邊的詳細片段因爲該項目選擇的條件沒有得到滿足

@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); 
    } 
} 

回答

1

在提交事務後,片段不會立即添加到佈局樹中。您必須等到片段被調用時的onViewCreated。您可能可以訪問onStart - 活動的方法中的視圖,但將該邏輯保留在片段本身內可能更好。活動不應該關注片段中包含哪些視圖。

+0

這就是訣竅!我將條件移至onStart並更改了setActivateOnItemClick(true)以使用私有成員字段mFrag並解決問題!非常感謝! – jamesc

1

我認爲這個問題是你要尋找的片段從活動,但因爲你不使用setContentView,活動實際上並沒有一個視圖透視。嘗試使用getView()方法從片段獲取視圖。

+0

謝謝你的回答,它給了我一個關於我需要做什麼的巨大線索。我接受了@TimoOhr答案,因爲它給了我所需的實際事件。恥辱我可以; t接受這兩個答案 – jamesc