Android tabhost error null pointer tabwidget - java

Mainactivity:
public class MainActivity extends AppCompatActivity {
TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabHost.TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
I am experimenting with tabs in order to make a bottom navigation bar without material design library.
How to fix this error? (Attempt to invoke virtual method 'void android.widget.TabWidget.setStripEnabled(boolean)' on a null object reference)

You need invoke mTabHost.setup(...);
The reason is your activity does not extends TabActivity, you should invoke TabHost.setup(...) before TabHost.addTab(...) method.

Related

Control layout object in fragment - getting null object reference

I made a fragment_console.xml and ConsoleFragment.java
In MainActivity.java I create object console_fragment = new ConsoleFragment();
Then OnClickListener runs console_fragment.method();
But it gives error Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
on line
sendButton = v.findViewById(R.id.fragment_console_send_button);
ConsoleFragment.java
public class ConsoleFragment extends Fragment {
View v;
private boolean isVisible;
private Button sendButton;
private TextView consoleText;
private EditText commandText;
public ConsoleFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_console, container, false);
showConsole(v);
return v;
}
public void showConsole(View v){
sendButton = (Button) v.findViewById(R.id.fragment_console_send_button);
consoleText = (TextView) v.findViewById(R.id.fragment_console_log_text);
commandText = (EditText) v.findViewById(R.id.fragment_console_command_text);
isVisible = true;
sendButton.setVisibility(View.GONE);
consoleText.setVisibility(View.GONE);
commandText.setVisibility(View.GONE);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ConsoleFragment console_fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
console_fragment = new ConsoleFragment();
}
public void triggerConsole(View view) { //buton on click
console_fragment.showConsole(view);
}
}
fragment_console.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activities.ConsoleFragment">
<TextView
android:id="#+id/fragment_console_log_text"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="TextView"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent">
<EditText
android:id="#+id/fragment_console_command_text"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.75"
android:layout_marginBottom="2dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/console_command"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/fragment_console_send_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.25"
android:text="#string/console_send_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">
<include
android:id="#+id/include"
layout="#layout/activity_top_bar"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="com.wordfall.wordfallcontroll.activities.ConsoleFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/include2"
app:layout_constraintStart_toStartOf="parent" />
</FrameLayout>
<include
android:id="#+id/include2"
layout="#layout/activity_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
How to get layout object (buttons, tv etc) to get control on it in fragment class?
public class XFragment extends Fragment {
View v;
private boolean isVisible;
private Button sendButton;
private TextView consoleText;
private EditText commandText;
public XFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_x, container, false);
showConsole(v);
return v;
}
public void showConsole(View v){
sendButton = v.findViewById(R.id.fragment_console_send_button);
consoleText = v.findViewById(R.id.fragment_console_log_text);
commandText = v.findViewById(R.id.fragment_console_command_text);
isVisible = true;
sendButton.setVisibility(View.GONE);
consoleText.setVisibility(View.GONE);
commandText.setVisibility(View.GONE);
}
}
In your activity_main.xml file you need to create a container for holding fragments like:
activity_main.xml
<LinearLayout
......
......
....
>
.........
........
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
In your MainActivity.java file inside onCreate method you can do:
MainActivity.java
#Override
protected void onCreate(...){
.....
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new ConsoleFragment(), "ConsoleFragment").addToBackStack(null).commit();
}
What view you pass into triggerConsole method from outside fragment? The problem may be in this, you'll try to find controls in layout, which is not part of your fragment. View of your fragment is assigned to global variable v, but now you try to get it from method parameter named v. Try this code.
public void showConsole(){
sendButton = (Button) v.findViewById(R.id.fragment_console_send_button);
consoleText = (TextView) v.findViewById(R.id.fragment_console_log_text);
commandText = (EditText) v.findViewById(R.id.fragment_console_command_text);
isVisible = true;
sendButton.setVisibility(View.GONE);
consoleText.setVisibility(View.GONE);
commandText.setVisibility(View.GONE);
}
If you want pass different layouts into same fragment, do it via constructor, not via method parameter, and each time make new instance of fragment.

Widget layout from Activity still appear after fragment add transaction

I have a problem with fragments. I add fragments dynamically without removing old fragments so they can be recalled when I return. But when I applied the following snippet I found that some of the views that came from the activity did not disappear, such as the Button, TextView, and so on. The results I get are overlapping views
MainActivity.Java
public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener {
private static final String TAG = "MainActivity";
private Unbinder unbinder;
private FragmentManager fragmentManager;
#BindView(R.id.dynamic_fragment_frame)
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading);
unbinder = ButterKnife.bind(this);
fragmentManager = this.getSupportFragmentManager();
}
public void openFragment(View view) {
BlankFragment fragment = BlankFragment.newInstance("Test 1");
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
transaction.addToBackStack(null);
transaction.add(R.id.dynamic_fragment_frame, fragment, "BLACK_FRAGMENT");
transaction.commit();
}
#Override
protected void onDestroy() {
unbinder.unbind();
super.onDestroy();
}
#Override
public void onFragmentInteraction(String text) {
Log.d(TAG, "onFragmentInteraction: " + text);
onBackPressed();
}
}
activity_reading.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00d9ff"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/dynamic_fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/title_read_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini Judul"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:paddingBottom="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="openFragment"
android:text="Open"
android:stateListAnimator="#null"/>
</RelativeLayout>
BlankFragment.java
public class BlankFragment extends Fragment {
private static final String ARG_TEXT = "TEXT";
private Unbinder unbinder;
private String mParam1;
private OnFragmentInteractionListener mListener;
#BindView(R.id.tv_blank_fragment)
TextView tvTitle;
#BindView(R.id.back_btn)
Button backBtn;
#BindView(R.id.next_btn)
Button nextBtn;
private FragmentManager fragmentManager;
public BlankFragment() {
// Required empty public constructor
}
public static BlankFragment newInstance(String param1) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, param1);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_TEXT);
}
fragmentManager = getFragmentManager();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
unbinder = ButterKnife.bind(this, view);
tvTitle.setText(mParam1);
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String test = "test dari fragment";
sendBack(test);
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
public void sendBack(String sendback) {
if (mListener != null) {
mListener.onFragmentInteraction(sendback);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(String text);
}
}
fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cfcf1d"
tools:context=".BlankFragment">
<TextView
android:id="#+id/tv_blank_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<Button
android:id="#+id/next_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/back_btn"
android:layout_alignParentStart="true"
android:text="Create new Fragment" />
<Button
android:id="#+id/back_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:text="back" />
</RelativeLayout>
Everything went smoothly but the result of my Mainactivity layout still appeared on fragment. Please help me
Result as shows as below:
here 1
here 2
In activity_reading, the fragment container view (dynamic_fragment_frame) is declared first, so the button and the text view will be displayed on top of it. If you move the fragment container to the bottom, the fragments will be displayed correctly, so the layout should look something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00d9ff"
tools:context=".MainActivity">
<TextView
android:id="#+id/title_read_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini Judul"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:paddingBottom="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="openFragment"
android:text="Open"
android:stateListAnimator="#null"/>
<FrameLayout
android:id="#+id/dynamic_fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00d9ff"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/dynamic_fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/title_read_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini Judul"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:paddingBottom="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="openFragment"
android:text="Open"
android:stateListAnimator="#null"/>
</RelativeLayout>
With the above layout, Imagine that the TextView and Button are over the FragmeLayout. So when you add a Fragment to the FrameLayout, the TextView and Button are still over the FragmeLayout. To hide these views, you should use TextView.setVisibility(View.GONE) and Button.setVisibility(View.GONE) when adding a Fragment

endAllActiveAnimators on 0xa2ee7500 (RippleDrawable) with handle 0xa1c126d0 in Android Fragment

I have this error on starting Fragment. Sometimes the fragment showing, sometime not, there should be only a blank screen showing.
At the time logcat shows "
endAllActiveAnimators on 0xa2ee7500 (RippleDrawable) with handle 0xa1c126d0"
Also Please describe the endAllActiveAnimators
The code is given below
public class SingleArtViewDetail extends BaseActivity {
#BindView(R.id.tabSingleartLayoutHome)
TabLayout tabLayout;
#BindView(R.id.viewPagerSingleartHome)
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
private ViewArtDetailFragment viewArtDetailFragment = new ViewArtDetailFragment();
private CommentsFragment commentsFragment = new CommentsFragment();
private SimilarArtFragment similarArtFragment = new SimilarArtFragment();
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_art_view_detail);
if(getIntent()!=null) {
EventBus.getDefault().postSticky(new ArtIdEvent(getIntent().getStringExtra("artid")));
}
ButterKnife.bind(this);
setToolBar("");
setTabs();
activity=SingleArtViewDetail.this;
// EventBus.getDefault().register(this);
}
#Override
protected void onDestroy() {
// EventBus.getDefault().unregister(this);
super.onDestroy();
}
private void setTabs() {
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFrag(viewArtDetailFragment, "");
viewPagerAdapter.addFrag(commentsFragment, "");
viewPagerAdapter.addFrag(similarArtFragment, "");
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
final int[] ICONS = new int[]{
R.drawable.ic_addtocart,
R.drawable.ic_comments,
R.drawable.ic_similaritems};
final int[] ICONSUNSELECTED = new int[]{
R.drawable.ic_addtocartselected,
R.drawable.ic_commentsselected,
R.drawable.ic_similaritemsselected};
tabLayout.getTabAt(0).setIcon(ICONS[0]);
tabLayout.getTabAt(1).setIcon(ICONSUNSELECTED[1]);
tabLayout.getTabAt(2).setIcon(ICONSUNSELECTED[2]);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
System.out.println("selected"+tab.getPosition());
switch (tab.getPosition())
{
case 0:
tabLayout.getTabAt(0).setIcon(ICONS[0]);
break;
case 1:
tabLayout.getTabAt(1).setIcon(ICONS[1]);
break;
case 2:
tabLayout.getTabAt(2).setIcon(ICONS[2]);
break;
}
// tab.setIcon(R.drawable.newicon);
//also you can use tab.setCustomView() too
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
//tab.setIcon(R.drawable.oldicon);
System.out.println("unselected"+tab.getPosition());
switch (tab.getPosition())
{
case 0:
tabLayout.getTabAt(0).setIcon(ICONSUNSELECTED[0]);
break;
case 1:
tabLayout.getTabAt(1).setIcon(ICONSUNSELECTED[1]);
break;
case 2:
tabLayout.getTabAt(2).setIcon(ICONSUNSELECTED[2]);
break;
}
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
System.out.println(tab);
}
});
}}
Fragment is
public class ViewArtDetailFragment extends BaseFragment {
Context context;
String artid;
SingleImageModel singleImageModel = new SingleImageModel();
SingleArtDetail singleArtDetail;
#BindView(R.id.layout_artdetail)
LinearLayout layout_artdetail;
#BindView(R.id.progressbar)
ProgressBar progressbar;
#BindView(R.id.pager)
ImageView viewPager;
#BindView(R.id.horizontalscrlview)
HorizontalScrollView horizontalscrlview;
#BindView(R.id.tv_arttitle)
TextView tv_arttitle;
#BindView(R.id.artbaseprice)
TextView tv_artbaseprice;
#BindView(R.id.tv_artownername)
TextView tv_artownername;
#BindView(R.id.tv_artdescrptn)
TextView tv_artdescrptn;
#BindView(R.id.tv_viewdimension)
TextView tv_viewdimension;
#BindView(R.id.tv_viewyearofcreation)
TextView tv_viewyearofcreation;
#BindView(R.id.tv_viewcategory)
TextView tv_viewcategory;
#BindView(R.id.tv_viewsubject)
TextView tv_viewsubject;
#BindView(R.id.tv_viewstyle)
TextView tv_viewstyle;
#BindView(R.id.tv_viewmaterials)
TextView tv_viewmaterials;
#BindView(R.id.tv_viewmedium)
TextView tv_viewmedium;
#BindView(R.id.tv_vieworiginal)
TextView tv_vieworiginal;
#BindView(R.id.tv_viewprint)
TextView tv_viewprint;
#BindView(R.id.thumbnails)
LinearLayout _thumbnails;
#BindView(R.id.tv_copyright)
TextView tv_copyright;
// private GalleryPagerAdapter _adapter;
private ArrayList<String> thumb_images = new ArrayList<>();
private ArrayList<String> gallery_images = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_art_details, container, false);
ButterKnife.bind(this, view);
context = getActivity();
ArtIdEvent artIdEvent = EventBus.getDefault().getStickyEvent(ArtIdEvent.class);
artid = artIdEvent.getArtId();
callingArtDetailApi();
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);}}
Fragment xml is given Below
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
>
<ProgressBar
android:id="#+id/progressbar"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:visibility="gone"
android:id="#+id/layout_artdetail"
android:layout_below="#+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginBottom="80dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_marginRight="#dimen/tabsmall_margin"
android:layout_marginLeft="#dimen/tabsmall_margin"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="#dimen/viewpagerheight"
>
</ImageView>
<HorizontalScrollView
android:id="#+id/horizontalscrlview"
android:layout_marginRight="#dimen/tabsmall_margin"
android:layout_marginLeft="#dimen/tabsmall_margin"
android:layout_width="match_parent"
android:layout_height="#dimen/horizonalscrollheight"
android:layout_gravity="bottom"
>
<LinearLayout
android:id="#+id/thumbnails"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="2dp">
</LinearLayout>
</HorizontalScrollView>
<include
layout="#layout/content_view_art_detail"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Please help to solve this issue, thanks in advance.
EDIT
Full log of the error (Posted by OP in comments):
06-29 13:06:55.702 24382-24382/com.cushbuart.cushbuart E/log: onsavedInstanceState 06-29 13:07:01.840 24382-24525/com.cushbuart.cushbuart D/OpenGLRenderer: endAllActiveAnimators on 0x9ecd8b80 (RippleDrawable) with handle 0xa19a4540 06-29 13:07:03.612 24382-24382/com.cushbuart.cushbuart D/EventBus: No subscribers registered for event class com.cushbuart.cushbuart.eventmodel.ArtIdEvent 06-29 13:07:03.612 24382-24382/com.cushbuart.cushbuart D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
solved my issue it is because of my tablayout xml file.
i changed that to
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
layout="#layout/toolbar"/>
<android.support.v4.view.ViewPager
android:layout_below="#+id/toolbar"
android:id="#+id/viewPagerSingleartHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabSingleartLayoutHome"
app:tabMode="fixed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#color/colorwhite"
app:tabIndicatorColor="#color/colorwhite"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

Problems with setting up tabs using Tabhost in Android?

So, i was following the NewBoston series on Android tutorials. So, there is this tutorial on setting up tabs using Tabhost and Tabspecs. Now, i tried exactly what the tutorial does but crashes and it points to a particular line in my code which is setContentView() method in the OnCreate(). I tried even removing it and ran the code but the program just crashes. What could be my mistake? I also tried the code in the following article to see if that works but it still crashes.
http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html
This is my code-
Splash.java is the class that starts up the Launcher activity
public class Splash extends TabActivity {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
th = (TabHost)findViewById(R.id.tabHost2);
TabHost.TabSpec tab1 = th.newTabSpec("First Tab");
TabHost.TabSpec tab2 = th.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = th.newTabSpec("Third tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,TabActivity1.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this, TabActivity2.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this, TabActivity3.class));
th.addTab(tab1);
th.addTab(tab2);
th.addTab(tab3);
}
}
TabActivity1.java-
public class TabActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab1 Activity");
setContentView(tv);
}
}
TabActivity2.java:-
public class TabActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab2 Activity");
setContentView(tv);
}
}
TabActivity3.java
public class TabActivity3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setTextSize(25);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText("This Is Tab3 Activity");
setContentView(tv);
}
}
splash.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">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost2"
android:layout_gravity="center_horizontal">
<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="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Change this line
th = (TabHost)findViewById(R.id.tabHost2);
to
th = (TabHost)findViewById(android.R.id.tabHost2);
This will do.
Because, in the layout file, you might have set Tabhost id like this android:id="#android:id/tabhost2">

How do I use tabHost for Android

I have looked at posts on Stack Overflow and at tutorials on other websites, and I cannot understand how to use TabHost. Can someone please explain it to me and maybe send me a link to a tutorial?
In ManiActivity extends TabActivity
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this ,FirstActivity.class )));
mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , SecondActivity.class )));
mTabHost.setCurrentTab(0);
}
}
In this activity not use layout "activity_main.xml" .
Tabhost mTabHost = getTabHost(); is create main tab.
mTabHost.newTabSpec("first") is create tabspec id "first".
setIndicator("First") is create text "First" in title tab.
setContent(new Intent(this ,FirstActivity.class )) is use content from FirstActivity.class ( FirstActivity.java )
mTabHost.addTab(....) is add spectab to main tab
mTabHost.setCurrentTab(0) is defult tab when start page.
FirstActivity.java
public class FirstActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.first_layout );
}
}
SecondActivity.java
public class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.second_layout );
}
}
"R.layout.first_layout" is content from first_layout.xml
"R.layout.second_layout" is content from second_layout.xml
In AndroidManifest.xml add activity name ".FirstActivity" and ".SecondActivity" in example xml.
Finish!!!!!
First of all while TabHost is not deprecated, TabActivity on other hand is deprecated due to Fragment API.
There are two ways to use TabHost; using Fragment via FragmentTabHost and using TabHost.TabContentFactory.
1. Using Fragment via FragmentTabHost
This sample code show you how to use TabHost in Activity.
FragmentTabHostActivity.java
public class FragmentTabHostActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tab_host_activity);
FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
fragmentTabHost.addTab(getTabSpec1(fragmentTabHost), Tab1Fragment.class, null);
fragmentTabHost.addTab(getTabSpec2(fragmentTabHost), Tab2Fragment.class, null);
}
private TabHost.TabSpec getTabSpec1(FragmentTabHost tabHost) {
return tabHost.newTabSpec("First Tab")
.setIndicator("Tab1");
}
private TabHost.TabSpec getTabSpec2(FragmentTabHost tabHost) {
return tabHost.newTabSpec("Second Tab")
.setIndicator("Tab 2");
}
}
fragment_tab_host_activity.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: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="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Actually by using Fragment, you can use Tab inside a Fragment (Android docs).
2. Using TabHost.ContentFactory
TabHostActivity.java
public class TabHostActivity extends AppCompatActivity implements TabHost.TabContentFactory {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(getTabSpec1(tabHost));
tabHost.addTab(getTabSpec2(tabHost));
}
private TabHost.TabSpec getTabSpec1(TabHost tabHost) {
return tabHost.newTabSpec("First Tab")
.setIndicator("Tab1")
.setContent(this);
}
private TabHost.TabSpec getTabSpec2(TabHost tabHost) {
return tabHost.newTabSpec("Second Tab")
.setIndicator("Tab 2")
.setContent(this);
}
#Override
public View createTabContent(String tag) {
return LayoutInflater.from(this).inflate(R.layout.activity_tab_1, null);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
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: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="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</TabHost>
However, I personally recommend using newest Material Design style TabLayout class.

Categories

Resources