Tab Content not showing on FragmentTabHost Android - android

I have tried using FragmentTabHost. all went well, and has managed to show the tab well. but my fragment not showing layout of that class. I've tried using the TextView, but still did not appear.
please help me.
My FragmentActivity
public class ViewDetailAnak extends FragmentActivity{
private static String idanak;
private FragmentTabHost tabHost;
private static final String TRANS_SPEC = "";
private static final String US_SPEC = "";
private static final String TABU_SPEC = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fro_single_item_anak);
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);
tabHost.addTab(
tabHost.newTabSpec(TRANS_SPEC)
.setIndicator(TRANS_SPEC, getResources().getDrawable(R.drawable.icon_trans)),
FROVDATransaksi.class, null);
tabHost.addTab(
tabHost.newTabSpec(US_SPEC)
.setIndicator(US_SPEC, getResources().getDrawable(R.drawable.icon_us)),
FROVDAUangsaku.class, null);
tabHost.addTab(
tabHost.newTabSpec(TABU_SPEC)
.setIndicator(TABU_SPEC, getResources().getDrawable(R.drawable.icon_tab)),
FROVDATabungan.class, null);
}
One of 3 fragment
public static class FROVDAUangsaku extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fro_vda_uangsaku, container, false);
return v;
}
}
and this is XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="14dp"
android:clipToPadding="true"
android:paddingTop="15dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/user1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="15.86"
android:orientation="vertical"
android:paddingLeft="10dp" >
<TextView
android:id="#+id/txtNama_FRO_Profil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/nama" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="10dp" >
<TextView
android:id="#+id/txNama_fra_profil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Uang Saku"
android:textColor="#666666"
android:textSize="9dp" />
<TextView
android:id="#+id/txtJumlahAnak_FRO_Profil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtnomuangsaku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Tabungan"
android:textColor="#666666"
android:textSize="9sp" />
<TextView
android:id="#+id/saldotabungan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnLogoutFRO_profilo"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="Edit Profile"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</TableRow>
<View
android:layout_height="2dip"
android:layout_marginTop="5dp"
android:background="#666666" />
</TableLayout>
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tableLayout1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="38dp" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
ignore inside TableLayout. The only design. and I make fragmenttab under TableLayout

Related

Getting NullPointerException when adding toolbar

Getting NullPointerExceptionwhen trying to add a toolbar which is been defined in a layout file other than main design xml file. I keep getting the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.Toolbar.findViewById(int)' on a null object reference
Here is my code
public class History_List_Details_Fragment extends Fragment {
Histroy_List_Model histroyListModel;
public History_List_Details_Fragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
histroyListModel = getArguments().getParcelable("historydetail");
String title=histroyListModel.getShared_title();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.history_list_details_fragment, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.app_bar);
initializeListnersForToolbar(toolbar);
initializeViews(rootView);
return rootView;
}
private void initializeViews(View rootView) {
TextView titleText = (TextView) rootView.findViewById(R.id.txtTitle);
titleText.setText(histroyListModel.getShared_title());
TextView messageTExt = (TextView) rootView.findViewById(R.id.txtMessage);
titleText.setText(histroyListModel.getShared_message());
}
private void initializeListnersForToolbar(Toolbar toolbar){
((TextView) toolbar.findViewById(R.id.tool_bar_text_head)).setText(getResources().getString(R.string.friends));
((ImageView) toolbar.findViewById(R.id.tool_bar_back_arrow)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment=new History_List_Fragment();
Utilities.getInstance(getActivity()).change_HomPage_Fragment(
fragment, "History_List_Fragment", getActivity());
}
});
}
}
History_List_Details_Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="#+id/txtTitle"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="#+id/txtMessage"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Duration"
android:id="#+id/txtDuration"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/lstMembers"></ListView>
</LinearLayout>
</LinearLayout>
How can I fix this error?
Add Toolbar in "history_list_details_fragment" with id "app_bar"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="#+id/txtTitle"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="#+id/txtMessage"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Duration"
android:id="#+id/txtDuration"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/lstMembers"></ListView>
</LinearLayout>
The error states clearly. Inside history_list_details_fragment.xml there is no Toolbar. Try to add it.
Or more probably the Toolbar is in you Activity layout, so init it inside Activity.

How to remove main activities buttons from the fragment with the codes in android?

How to remove main activities buttons from the fragment with the codes in android?
There are a few buttons in activity_main.xml. I want to access and remove a button from the fragment class. Is this posssible and how?
main_activ.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/paper"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="horizontal" >
<Button
android:id="#+id/butBack"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_weight="0.5"
android:text=".."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
<Button
android:id="#+id/butMain"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_marginLeft="2dp"
android:layout_weight="0.5"
android:text="..."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
<Button
android:id="#+id/butNext"
android:layout_width="wrap_content"
android:layout_height="60.0dip"
android:layout_gravity="fill"
android:layout_marginLeft="2dp"
android:layout_weight="0.5"
android:text=".."
android:textColor="#ffffffff"
android:textSize="16.0sp"
android:background="#drawable/button_style"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My fragment
public class Frag_Menu extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_menu, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) getActivity().findViewById(R.id.butBack).getParent();
if(null!=layout)
layout.removeView(layout);
};
public static android.support.v4.app.Fragment newInstance(String string) {
return null;
}
}
You can use getActivity().findViewById() to get your Button from the fragment class in onActivityCreated().
View button = getActivity().findViewById(R.id.butBack);
if (button != null && button.getParent() != null) {
((ViewGroup)button.getParent()).removeView(button);
}

It show the java.lang.NullPointerException when set the framelayout to VISIBLE in android

I detect the WIFI connect , when WIFI connect is break.
It will show the framelayout on the top of all widgets.
public class MjpegPlayerFragment extends Fragment {
public static FrameLayout reconnect_layout;
private class GetTimeStamp extends AsyncTask<URL, Integer, String> {
protected void onPostExecute(String result) {
Activity activity = getActivity() ;
if (activity != null) {
reconnect_fragment();
}
super.onPostExecute(result) ;
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.preview_player, container, false) ;
reconnect_layout = (FrameLayout) getActivity().findViewById(R.id.reconnect_layout);
return view ;
}
public static void reconnect_fragment() {
reconnect_layout.setVisibility(View.VISIBLE);
}
And the code of xml is like the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/reconnect_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:background="#454749">
<RelativeLayout
android:id="#+id/layout_imgbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_above="#+id/bottom_buttonlayout"
android:layout_below="#+id/top_buttonlayout"
android:orientation="horizontal" >
<ImageView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/connect"
android:layout_centerVertical="true"
android:background="#drawable/phone"/>
<ImageView
android:id="#+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/break1"/>
<ImageView
android:id="#+id/break_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/connect"
android:layout_centerVertical="true"
android:background="#drawable/camera_break"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/Reconnect_button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_below="#+id/layout_break_text"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/Imgbtn_reconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:background="#00000000"
android:src="#drawable/reconnect" />
</LinearLayout>
<FrameLayout
android:id="#+id/player_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/player_surface_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<SurfaceView
android:id="#+id/player_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/top_buttonlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#444444" >
<ImageView
android:id="#+id/imageBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#drawable/logo"
/>
<ImageButton
android:id="#+id/SettingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:background="#drawable/parmeter_setting" />
<ImageButton
android:id="#+id/FileSavebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:background="#drawable/save_in_camera" />
</LinearLayout>
</RelativeLayout>
When it call the reconnect_fragment(); it show the java.lang.NullPointerException at reconnect_layout.setVisibility(View.VISIBLE);
Does there has any wrong ?
How to solve this problem ?
As you said in your comment framelayout is in fragment layout
So Change to
reconnect_layout = (FrameLayout) view.findViewById(R.id.reconnect_layout);
If it is in activity xml you use getActivity().findViewById
replace
reconnect_layout = (FrameLayout) getActivity().findViewById(R.id.reconnect_layout);
with
reconnect_layout = (FrameLayout) view.findViewById(R.id.reconnect_layout);

Put a tabhost inside a fragment?

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)'?
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);

how to inflate a list view to a fragment

i am using fragment in my application.there are 5 tabs.in 1 tab i need to inflate a list view on a button click.given below is my code for that tab.
public class TabStoreLocatorFragment extends Fragment implements OnClickListener {
private Button mapSwitcher;
private LinearLayout lastSelectedStoreButton;
private LinearLayout noSelectedStoreSection;
private TextView storeInfoName;
private TextView storeInfoAddress;
private TextView storeInfoCity;
private RelativeLayout phoneLocationButton;
private EditText searchStoreLocation;
private ListView storeList;
private ViewFlipper storeFlipper;
LinearLayout theLayout;
Store store;
#SuppressWarnings("static-access")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
theLayout = (LinearLayout) inflater.inflate(
R.layout.tab_store_locator_layout, container, false);
phoneLocationButton = (RelativeLayout) theLayout.findViewById(R.id.phone_location_button);
mapSwitcher=(Button)theLayout.findViewById(R.id.switch_state_button);
lastSelectedStoreButton=(LinearLayout)theLayout.findViewById(R.id.last_selected_store_button);
noSelectedStoreSection=(LinearLayout)theLayout.findViewById(R.id.no_selected_store);
storeInfoName=(TextView)theLayout.findViewById(R.id.store_name);
storeInfoAddress=(TextView)theLayout.findViewById(R.id.store_address);
storeInfoCity=(TextView)theLayout.findViewById(R.id.store_city);
searchStoreLocation=(EditText)theLayout.findViewById(R.id.search_store);
storeFlipper = (ViewFlipper)theLayout.findViewById(R.id.store_flipper);
store=new Store();
store.setName("store1");
store.setAddress("california");
store.setCity("newyork");
store.setZipCode("23");
store.setState("california");
phoneLocationButton.setOnClickListener(this);
mapSwitcher.setOnClickListener(this);
lastSelectedStoreButton.setOnClickListener(this);
return theLayout;
}
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.phone_location_button) {
storeList = (ListView)storeFlipper.inflate(getActivity(), R.layout.store_list, storeFlipper);
storeFlipper.addView(storeList);
StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store_list_item);
storeList.setAdapter(adapter);
}
else if(id == R.id.switch_state_button){
}
else{
}
}
}
i want to inflate storelist to storeFlipper.i tried to do that.but it is not working.given below is the corresponding layout for that tab.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/find_a_store_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:text="#string/tab_find_a_store_title"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<Button android:id="#+id/switch_state_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/switch_selector" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E2D3" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#C5BFAD" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/last_selected_store_button" android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#android:style/Widget.Button"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#8E8672"
android:text="#string/last_selected_store_label"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView android:id="#+id/store_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
<TextView android:id="#+id/store_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/arrow" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:id="#+id/no_selected_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:background="#drawable/last_selected_store_background" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#8E8672"
android:text="#string/no_selected_store_label"
android:textSize="12sp" />
</LinearLayout>
</FrameLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<EditText android:id="#+id/search_store"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:singleLine="true"
android:textSize="14sp"
android:hint="#string/search_store_placeholder"
android:layout_toLeftOf="#+id/phone_location_button"
android:background="#drawable/search_store_background" />
<RelativeLayout android:id="#id/phone_location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/get_location_background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/get_location" />
</RelativeLayout>
</RelativeLayout>
<ViewFlipper android:id="#+id/store_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
can anybody help me to inflate the list to flipper.
it is simple.just put this code.
storeList = (ListView) inflater.inflate(R.layout.store_list, null);
it is working

Categories

Resources