本文介绍了Android的添加选项卡里面的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加一个TabHost内的片段......在code被bellow.Here的片段我想补充TabHost以显示两个选项卡里面
公共类TablesFragment扩展片段{
TabHost tabHost;
TabHost.TabSpec规范;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.tables_fragment,集装箱,
假);
tabHost =(TabHost)rootView.findViewById(android.R.id.tabhost);
tabHost.setup();
意图intentAndroid =新意图()。setClass(getActivity()
NewsFragment.class);
规格= tabHost.newTabSpec(机器人)。setContent(intentAndroid)
.setIndicator(机器人);
tabHost.addTab(规范);
。意图intentBus =新意图()setClass(getActivity(),NewsFragment.class);
规格= tabHost.newTabSpec(欢迎)。setIndicator(欢迎)
.setContent(intentBus).setIndicator(欢迎);
tabHost.addTab(规范);
返回rootView;
}
}
这是给我的错误...需要助理与此code ...
LogCat中:
01-26 16:21:27.092:E / AndroidRuntime(5597):致命异常:主要
01-26 16:21:27.092:E / AndroidRuntime(5597):java.lang.IllegalStateException:你忘了叫'公共无效设置(LocalActivityManager的ActivityGroup)?
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:692)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabHost.setCurrentTab(TabHost.java:358)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabHost.addTab(TabHost.java:236)
01-26 16:21:27.092:E / AndroidRuntime(5597):在com.fifaworldcup2014.TablesFragment.onCreateView(TablesFragment.java:31)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-26 16:21:27.092:E / AndroidRuntime(5597):在com.fifaworldcup2014.MainActivity.onTabChanged(MainActivity.java:164)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabHost.setCurrentTab(TabHost.java:376)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabHost $ 2.onTabSelectionChanged(TabHost.java:150)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.widget.TabWidget $ TabClickListener.onClick(TabWidget.java:546)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.view.View.performClick(View.java:4084)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.view.View $ PerformClick.run(View.java:16966)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.os.Handler.handleCallback(Handler.java:615)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.os.Handler.dispatchMessage(Handler.java:92)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.os.Looper.loop(Looper.java:137)
01-26 16:21:27.092:E / AndroidRuntime(5597):在android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 16:21:27.092:E / AndroidRuntime(5597):在java.lang.reflect.Method.invokeNative(本机方法)
01-26 16:21:27.092:E / AndroidRuntime(5597):在java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:21:27.092:E / AndroidRuntime(5597):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 16:21:27.092:E / AndroidRuntime(5597):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 16:21:27.092:E / AndroidRuntime(5597):在dalvik.system.NativeStart.main(本机方法)
解决方案
我相信这会为你工作,如果你的目标API17 +。如果没有,你应该看看ViewContainers,和swipeable景观。
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentTabHost;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类TabHostParentFragment扩展片段{
私人FragmentTabHost tabHost;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
tabHost =新FragmentTabHost(getActivity());
tabHost.setup(getActivity(),getChildFragmentManager(),R.layout.my_parent_fragment);
捆绑ARG1 =新包();
arg1.putInt(精氨酸为Frag1,1);
tabHost.addTab(tabHost.newTabSpec(TAB1)。setIndicator(破片TAB1),
NewsFragment.class,ARG1);
捆绑ARG2 =新包();
arg2.putInt(精氨酸为Frag2,2);
tabHost.addTab(tabHost.newTabSpec(TAB2)。setIndicator(破片TAB2),
MyNestedFragment2.class,ARG2);
返回tabHost;
}
I am Trying to add a TabHost Inside a Fragment ...The Code is given bellow.Here inside the Fragment I am Trying to add TabHost to show two Tab
public class TablesFragment extends Fragment {
TabHost tabHost;
TabHost.TabSpec spec;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tables_fragment, container,
false);
tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
tabHost.setup();
Intent intentAndroid = new Intent().setClass(getActivity(),
NewsFragment.class);
spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
.setIndicator("Android");
tabHost.addTab(spec);
Intent intentBus = new Intent().setClass(getActivity(), NewsFragment.class);
spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
.setContent(intentBus).setIndicator("Welcome");
tabHost.addTab(spec);
return rootView;
}
}
This Is Giving me Error...Need Assistant with this code...
LogCat:
01-26 16:21:27.092: E/AndroidRuntime(5597): FATAL EXCEPTION: main
01-26 16:21:27.092: E/AndroidRuntime(5597): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:692)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabHost.setCurrentTab(TabHost.java:358)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabHost.addTab(TabHost.java:236)
01-26 16:21:27.092: E/AndroidRuntime(5597): at com.fifaworldcup2014.TablesFragment.onCreateView(TablesFragment.java:31)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-26 16:21:27.092: E/AndroidRuntime(5597): at com.fifaworldcup2014.MainActivity.onTabChanged(MainActivity.java:164)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabHost.setCurrentTab(TabHost.java:376)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.view.View.performClick(View.java:4084)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.view.View$PerformClick.run(View.java:16966)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.os.Handler.handleCallback(Handler.java:615)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.os.Looper.loop(Looper.java:137)
01-26 16:21:27.092: E/AndroidRuntime(5597): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 16:21:27.092: E/AndroidRuntime(5597): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:21:27.092: E/AndroidRuntime(5597): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:21:27.092: E/AndroidRuntime(5597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 16:21:27.092: E/AndroidRuntime(5597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 16:21:27.092: E/AndroidRuntime(5597): at dalvik.system.NativeStart.main(Native Method)
解决方案
I believe this could work for you, if you are targeting API17 +.If not you should take a look at ViewContainers, and swipeable views.
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabHostParentFragment extends Fragment {
private FragmentTabHost tabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
NewsFragment.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
MyNestedFragment2.class, arg2);
return tabHost;
}
这篇关于Android的添加选项卡里面的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!