0

I try to desin a screen , which is a layout of a fragment like this

https://dl.dropboxusercontent.com/u/37599516/Untitled.png

Or like this in instagram

https://dl.dropboxusercontent.com/u/37599516/Screenshot%202014-01-09%2010.27.19.png

i try this code :

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:layout_marginRight="5dip"
    android:background="@drawable/image_bg"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/userAvatar"
        android:layout_width="wrap_content"
        android:layout_height="89dp"
        android:src="@drawable/test_avatar" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/thumbnail"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_below="@+id/relativeLayout1"
    android:layout_toRightOf="@+id/thumbnail"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="soloco bolo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/description_section_icon" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="123456"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_menu_add" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="123dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/thumbnail" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="Đà Nẵng"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:gravity="center_horizontal|center_vertical"
            android:text="Activate"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal|center_vertical"
            android:text="6/1/2013-7/1/2013"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:id="@+id/containerTabhost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/relativeLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/linearLayout1"
    android:background="@drawable/backgroundactionbar"
    android:orientation="vertical" >
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.30" >

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="92dp" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/tab1list_place_list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:divider="#b5b5b5"
                        android:dividerHeight="1dp"
                        android:listSelector="@drawable/list_selector" >
                    </ListView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/tab2list_place_list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:divider="#b5b5b5"
                        android:dividerHeight="1dp"
                        android:listSelector="@drawable/list_selector" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/tab3list_place_list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:divider="#b5b5b5"
                        android:dividerHeight="1dp"
                        android:listSelector="@drawable/list_selector" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

and using this code to work with tab host in my fragment class

 public class HostFragment extends Fragment {
private FragmentTabHost mTabHost;

public HostFragment() {
    // Empty constructor required for fragment subclasses
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_host_layout,
            container, false);

    setHasOptionsMenu(true);
     TabHost mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);

         mTabHost.setup( );
     TabSpec photospec = mTabHost.newTabSpec("Search");
     photospec.setIndicator("Search",
     getResources().getDrawable(R.drawable.action_search));
     Intent searchIntent = new Intent(getActivity(),
     HostViewIntroduce.class);
     photospec.setContent(searchIntent);

     TabSpec songspec = mTabHost.newTabSpec("Favorites");
     songspec.setIndicator("Favorites",
     getResources().getDrawable(R.drawable.action_search));
     Intent favoriteIntent = new Intent(getActivity(),
     HostViewTips.class);
     songspec.setContent(favoriteIntent);

 mTabHost.addTab(songspec);
 mTabHost.addTab(photospec);

    return rootView;

}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    return super.onOptionsItemSelected(item);
}

 }

and it throw nullpointer exception

01-09 13:40:31.781: E/AndroidRuntime(5871): FATAL EXCEPTION: main
01-09 13:40:31.781:   E/AndroidRuntime(5871): java.lang.IllegalStateException: Did you forget to call 'public void    setup(LocalActivityManager activityGroup)'?
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
ajkala
  • 93
  • 1
  • 9
  • Quoting your own error message: `Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?` – WarrenFaith Jan 09 '14 at 11:52
  • Check this http://stackoverflow.com/questions/18120510/dynamically-changing-the-fragments-inside-a-fragment-tab-host/19859871#19859871 – AndroidHacker Jan 09 '14 at 11:52
  • @WarrenFaith I have no idea what does this question is. I try to google it but I can not find anything to solved my problem. – ajkala Jan 09 '14 at 12:13
  • Searching for "public void setup(LocalActivityManager activityGroup)" and I found this answer: http://stackoverflow.com/a/3641250/180538 – WarrenFaith Jan 09 '14 at 12:15
  • @AndroidHacker yes , I googled and found that question before . But It's different with my situation , He uses FragmentActivity . My problem is also easily solved by using FragmentActivity But I must use Fragment, that's it , not fragmentactivity . And we know that they are different a lot. – ajkala Jan 09 '14 at 12:16
  • @ajkala All fragments are wrapped in single Activity .. Make sure for that. :) – AndroidHacker Jan 09 '14 at 12:19
  • @WarrenFaith Yes , I also founf this question , but My Class must extend From Fragment and it can not extend from anymore supper class – ajkala Jan 09 '14 at 12:19
  • @AndroidHacker I know .My Main activity use different fragment to display different content , and My code below is one of them. And my code in Main activity Required that My SubActivity must be fragment – ajkala Jan 09 '14 at 12:22
  • @ajkala but have you called the setup(getActivity()) method?! What is your parent Activity were you place the fragment in? – WarrenFaith Jan 09 '14 at 15:10
  • @WarrenFaith here is my mainActivity http://pastebin.com/TH1hmi4E and here is my main layout http://pastebin.com/GWVUY3AM – ajkala Jan 10 '14 at 00:10

1 Answers1

1

You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.

Replace

 TabSpec photospec = mTabHost.newTabSpec("Search");
 photospec.setIndicator("Search",
 getResources().getDrawable(R.drawable.action_search));
 Intent searchIntent = new Intent(getActivity(),
 HostViewIntroduce.class);
 photospec.setContent(searchIntent);

 TabSpec songspec = mTabHost.newTabSpec("Favorites");
 songspec.setIndicator("Favorites",
 getResources().getDrawable(R.drawable.action_search));
 Intent favoriteIntent = new Intent(getActivity(),
 HostViewTips.class);
 songspec.setContent(favoriteIntent);

with

  TabHost.TabSpec photospec = mTabHost.newTabSpec("Search");
  photospec.setIndicator("Search",
  getResources().getDrawable(R.drawable.action_search));
  photospec.setIndicator(rootView);
  mTabHost.addTab(photospec, HostViewIntroduce.class, null);


  TabSpec songspec = mTabHost.newTabSpec("Favorites");
  songspec.setIndicator("Favorites",
  getResources().getDrawable(R.drawable.action_search));
  songspec.setIndicator(rootView);
  mTabHost.addTab(songspec, HostViewTips.class, null);
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • I don't get your answer. So i need to write my own custom FragmentTabHost Class , right ? – ajkala Jan 09 '14 at 12:00
  • No you have to just use this in your **HostFragment** – Piyush Jan 09 '14 at 12:02
  • I replace arcoding to yor answer but there is an error at addTab() : The method addTab(TabHost.TabSpec) in the type TabHost is not applicable for the arguments (TabHost.TabSpec, Class, null) – ajkala Jan 09 '14 at 12:09
  • @PiyushGupta I have same issue like this..can you help?http://stackoverflow.com/questions/28106944/how-to-add-tabhost-with-navigation-drawer –  Jan 23 '15 at 11:35