【问题标题】:Android Fragment, No view found for id inside TabHostAndroid 片段,在 TabHost 中找不到 id 的视图
【发布时间】:2023-03-05 18:49:01
【问题描述】:

我有一个使用 Tabhost 和 Fragments 设置选项卡的简单 Android 应用程序。在其中一个标签页上,片段是一个列表视图。我想要做的是,当用户单击列表视图中的一项时,根据单击的项目打开一个包含更具体信息的新视图,但保留它以便选项卡仍然存在。

我的方法是当用户单击列表视图中的项目时,会创建一个新片段,然后从拥有所有这些片段并处理选项卡逻辑的主 FragmentActivity 类中,它将分离当前显示的片段并添加这个“新”(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/

我的代码: active_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;
}

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-widget android-fragments android-tabhost


    【解决方案1】:

    我意识到这个问题是不久前被问到的,我正在为可能遇到这个问题的其他人发布一个答案。

    据我所知,没有“realtabcontent”的Android id。在您的 XML 中,删除 FrameLayout 的 id 的“android”部分。

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

    developer documentation上贴出的完整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>
    

    祝你好运,编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多