簡體   English   中英

Android Fragment,在TabHost中找不到id的視圖

[英]Android Fragment, No view found for id inside TabHost

我有一個簡單的Android應用程序,其中使用Tabhost和Fragments設置了標簽。 在其中一個標簽頁上,片段是列表視圖。 我想要做的是當用戶點擊列表視圖中的一個項目時,根據單擊的項目打開一個包含更多特定信息的新視圖,但保留它以使選項卡仍然存在。

我的方法是當用戶點擊listview中的項目,創建一個新片段,然后從我擁有所有這些片段並處理Tab鍵邏輯的主FragmentActivity類中,它將分離當前顯示的片段並添加“new” “(singleStationListItem)片段。 我遇到的問題是我無法使用R.id.realtabcontent將我的新片段添加到fragmentTransaction,因此,它沒有正確添加。 我明白了:

03-21 10:50:01.862:E / FragmentManager(1136):找不到用於片段singleStationListItem {40d090a0#2 id = 0x1010000}的id 0x1010000(android:attr / theme)的視圖

其中R.id.realtabcontent = 0x01010000;

我可以在沒有Id的情況下附加它,但是用戶無法返回(調用addtobackstash())。 我已經搜索了這個錯誤,但其他解決方案與tabhost沒有關系,我似乎做了所需要的,比如在onCreate()中調用setContextView(...)到包含我的framelayout標簽的布局的xml文件試圖將我的片段附加到。 我可以將我的選項卡片段添加到R.id.realtabcontent而不會出現問題。 有誰知道什么可能導致我的錯誤以及我如何解決它?

注意:我按照本教程使我的標簽工作。 我的代碼非常相似。 http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

我的代碼:activiy_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_body"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"
                android:padding="5dp" />

            <FrameLayout
                android:id="@+android:id/realtabcontent"  <--id that I want to set to
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="-4dp"
                android:layout_weight="0"
                android:orientation="horizontal" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

main_activity.java //來自我的片段活動類的部分

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

        // Step 1: Inflate layout
        setContentView(R.layout.activity_main);
        // Step 2: Setup TabHost
        initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
    }

public void onTabChanged(String tag) { //handles tab changes
    TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
    if (mLastTab != newTab) {
        FragmentTransaction ft =     this.getSupportFragmentManager().beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(this,
                        newTab.clss.getName(), newTab.args);
                ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); //note it uses the R.id.realtabcontent without any issues here
            } else {
                ft.attach(newTab.fragment);
            }
        }

        Log.d("fragment manager in activity: ", "" + ft.isEmpty());
        mLastTab = newTab;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
        Log.d("ft ID: ", ft.toString());
    }
}

//This is the callback function inside the listview fragment. I created an interface and it is overwritten in here as suggested by the Android SDK guide
public void onStationSelected(String station) {
    FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
    ft.addToBackStack(null);
    ft.addToBackStack(null);

    //create argument for new fragment that will be created
    Bundle b = new Bundle();
    b.putString("station", station);
    Fragment displayStationInfo = Fragment.instantiate(this, singleStationListItem.class.getName(), b);

    Fragment cur = getSupportFragmentManager().findFragmentById(R.id.realtabcontent);
    ft.detach(cur);
    ft.add(R.id.realtabcontent, displayStationInfo); <-- this line causes the error
    ft.commit();
    this.getSupportFragmentManager().executePendingTransactions();
}

singleStationListItem.java在這里

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle b = this.getArguments();

    getActivity().setContentView(R.layout.single_station_item_view);

    TextView txtStation = (TextView)   getActivity().findViewById(R.id.station_label);

    String station = b.getString("station");
    // displaying selected product name
    Log.d(TAG, "passed in station information: " + station);
    txtStation.setText(station);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = (LinearLayout)inflater.inflate(R.layout.single_station_item_view, container, false);
    return v;
}

任何幫助將不勝感激。

我意識到這個問題剛才被問到,我正在為可能遇到這個問題的其他人發布答案。

據我所知,沒有'realtabcontent'的Android ID。 在您的XML中,刪除您的ID的'android'部分為FrameLayout

<FrameLayout
    android:id="@+id/realtabcontent"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

開發人員文檔中發布的完整XML代碼如下:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

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

</LinearLayout>

祝你好運,編碼愉快!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM