I have a simple Android app with tabs configured with Tabhost and Fragments. On one of the tab pages, a snippet is a list. What I want to do is when the user clicks on one of the list items, open a new view with more specific information based on the click, but keeping it so that the tabs are still there.
My approach is when the user clicks on an item in the list, a new fragment is created, and then from my main class FragmentActivity, which owns all these fragments and processes the tabulation logic, it separates the currently displayed fragment and adds this βnewβ ( singleStationListItem) fragment. The problem I am facing is that I cannot add my new fragment to the fragmented interaction with the contents of R.id.realtabcontent and thus it will not be correctly added. I get:
03-21 10: 50: 01.862: E / FragmentManager (1136): no views were found for id 0x1010000 (android: attr / theme) for fragment singleStationListItem {40d090a0 # 2 id = 0x1010000}
where R.id.realtabcontent = 0x01010000;
I can attach it without Id, but then users will not be able to return (call addtobackstash ()). I searched for these errors, but other solutions do not apply to tabhost, and I seem to be doing what is required, for example call SetContextView (...) in onCreate () in an XML layout file that contains the framelayout tag I'm trying to attach my fragment. I can add tab fragments to the R.id.realtabcontent file without any problems. Does anyone know what might cause my error and how can I fix it?
Note. I follow this guide to work with my tabs. My code is very similar. http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
My code: 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//part from fragment activity class
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 here
@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; }
Any help would be greatly appreciated.