Android的错误:没有标签的空标签知 [英] Android Error : No Tab known for Null Tag
问题描述
我需要在顶部的固定区域(约20%),然后3下方标签。 XML是
mylist.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.2
机器人:方向=垂直> <查看
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.05
机器人:知名度=隐形/> <的TextView
机器人:ID =@ + ID / LIST_NAME
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.3
机器人:文字颜色=@彩色/ loclistitem_text
机器人:文字样式=大胆/> <的TextView
机器人:ID =@ + ID / list_spec
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.3/> <查看
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.1
机器人:知名度=隐形/> <的TextView
机器人:ID =@ + ID / list_user
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.2/>
< / LinearLayout中> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.8
机器人:方向=横向> <查看
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.05
机器人:知名度=隐形/> <的FrameLayout
机器人:ID =@ + ID / list_realtabcontent
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.95/> <查看
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.05
机器人:知名度=隐形/>
< / LinearLayout中> < android.support.v4.app.FragmentTabHost
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=#CCF8FB> <的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =0dip
机器人:layout_height =0dip
机器人:layout_weight =0/>
< /android.support.v4.app.FragmentTabHost>< / LinearLayout中>
MyListFragment.java
公共类MyListFragment扩展片段{ 私人FragmentTabHost mTabHost; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根 查看rootView = inflater.inflate(R.layout.mylist,集装箱,
假);
TextView的名字=(TextView中)rootView.findViewById(R.id.list_name);
TextView的规格=(TextView中)rootView.findViewById(R.id.loc_spec);
TextView的用户=(TextView中)rootView.findViewById(R.id.list_user);
name.setText(AppSession.getInstance()的getData()的getName());
mTabHost =新FragmentTabHost(getActivity()); mTabHost.setup(getActivity(),getChildFragmentManager(),
R.id.list_realtabcontent); 捆绑ARG1 =新包();
arg1.putInt(TAB1,1); mTabHost.addTab(
mTabHost.newTabSpec(TAB1)。setIndicator(TAB1
getResources()。getDrawable(R.drawable.tab_left))
Tab1.class,ARG1); 捆绑ARG2 =新包();
arg2.putInt(TAB2,2);
mTabHost.addTab(
mTabHost.newTabSpec(TAB2)。setIndicator(TAB2,
getResources()。getDrawable(R.drawable.tab_middle))
Tab2.class,ARG2); 捆绑ARG3 =新包();
arg3.putInt(TAB3,3);
mTabHost.addTab(
mTabHost.newTabSpec(TAB3)。setIndicator(TAB3,
getResources()。getDrawable(R.drawable.tab_rigth))
Tab3Fragment.class,ARG2); 返回rootView; } @覆盖
公共无效onDetach(){
super.onDetach(); 尝试{
现场childFragmentManager = Fragment.class
.getDeclaredField(mChildFragmentManager);
childFragmentManager.setAccessible(真);
childFragmentManager.set(这一点,空); }赶上(NoSuchFieldException E){
抛出新的RuntimeException(E);
}赶上(IllegalAccessException E){
抛出新的RuntimeException(E);
}
} }
我得到没有标签闻名标签空在android.support.v4.app.FragmentTabHost.doTabChanged。
如果我更改错误解决返回rootView
到返回mTabHost
。但随后在顶部的固定区域没有显示。它仅在屏幕顶部显示3个标签。
我试着用活动来设置这个特殊的屏幕和它工作得很好(固定区域顶部和标签正确显示),但我需要它使用片段作为我公司采用抽屉式导航所以这是片段。请指点。
修改的XML
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#CCF8FB> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / loc_playlist_locname
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.2/> <的FrameLayout
机器人:ID =@ + ID / playlist_realtabcontent
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.1/> <的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =0dip
机器人:layout_height =0dip
机器人:layout_weight =0/>
< / LinearLayout中>< /android.support.v4.app.FragmentTabHost>
我对您的code一些变化:
- 在实施中,使用在布局中指定的FragmentTabHost
- 使用的FrameLayout FragmentTabHost元素中指定在TabHost设置方法的布局。它似乎工作,检查出code如下:
XML文件:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.2
机器人:方向=垂直>
<查看
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.05
机器人:知名度=隐形/>
<的TextView
机器人:ID =@ + ID / LIST_NAME
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.3
机器人:文字样式=大胆/>
<的TextView
机器人:ID =@ + ID / list_spec
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.3/>
<查看
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.1
机器人:知名度=隐形/>
<的TextView
机器人:ID =@ + ID / list_user
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.2/>
< / LinearLayout中>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =0.8
机器人:方向=横向>
<查看
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.05
机器人:知名度=隐形/>
<查看
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.05
机器人:知名度=隐形/>
< / LinearLayout中>
< android.support.v4.app.FragmentTabHost
机器人:ID =@ + ID / tabhost
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=#CCF8FB>
<的FrameLayout
机器人:ID =@ + ID / list_realtabcontent
机器人:layout_width =0dp
机器人:layout_height =match_parent
机器人:layout_weight =0.95/>
< /android.support.v4.app.FragmentTabHost>
< / LinearLayout中>
片段类:
公共类MyListFragment扩展片段{ 私人FragmentTabHost mTabHost; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根 查看rootView = inflater.inflate(R.layout.activity_main,集装箱,
假);
TextView的名字=(TextView中)rootView.findViewById(R.id.list_name);
TextView的规格=(TextView中)rootView.findViewById(R.id.list_spec);
TextView的用户=(TextView中)rootView.findViewById(R.id.list_user);
mTabHost =(FragmentTabHost)rootView.findViewById(R.id.tabhost); mTabHost.setup(getActivity(),getChildFragmentManager(),
R.id.list_realtabcontent); 捆绑ARG1 =新包();
arg1.putInt(TAB1,1); mTabHost.addTab(
mTabHost.newTabSpec(TAB1)。setIndicator(TAB1
getResources()。getDrawable(R.drawable.tab_chat_unselected))
TabFragment.class,ARG1); 捆绑ARG2 =新包();
arg2.putInt(TAB2,2);
mTabHost.addTab(
mTabHost.newTabSpec(TAB2)。setIndicator(TAB2,
getResources()。getDrawable(R.drawable.tab_chat_unselected))
TabFragment.class,ARG2); 捆绑ARG3 =新包();
arg3.putInt(TAB3,3);
mTabHost.addTab(
mTabHost.newTabSpec(TAB3)。setIndicator(TAB3,
getResources()。getDrawable(R.drawable.tab_chat_unselected))
TabFragment.class,ARG3); 返回rootView; }
I need a fixed area (around 20%) at top and then 3 tabs below it. The xml is
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.05"
android:visibility="invisible" />
<TextView
android:id="@+id/list_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textColor="@color/loclistitem_text"
android:textStyle="bold" />
<TextView
android:id="@+id/list_spec"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:visibility="invisible" />
<TextView
android:id="@+id/list_user"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
<FrameLayout
android:id="@+id/list_realtabcontent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.95" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCF8FB" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
MyListFragment.java
public class MyListFragment extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.mylist, container,
false);
TextView name = (TextView) rootView.findViewById(R.id.list_name);
TextView spec = (TextView) rootView.findViewById(R.id.loc_spec);
TextView users = (TextView) rootView.findViewById(R.id.list_user);
name.setText(AppSession.getInstance().getData().getName());
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.id.list_realtabcontent);
Bundle arg1 = new Bundle();
arg1.putInt("Tab1", 1);
mTabHost.addTab(
mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
getResources().getDrawable(R.drawable.tab_left)),
Tab1.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Tab2", 2);
mTabHost.addTab(
mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
getResources().getDrawable(R.drawable.tab_middle)),
Tab2.class, arg2);
Bundle arg3 = new Bundle();
arg3.putInt("Tab3", 3);
mTabHost.addTab(
mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
getResources().getDrawable(R.drawable.tab_rigth)),
Tab3Fragment.class, arg2);
return rootView;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
I get "No Tab known for Tag null" at android.support.v4.app.FragmentTabHost.doTabChanged.
The error is resolved if I change return rootView
to return mTabHost
. But then the fixed area at top isn't showing. Its only displaying 3 tabs at the top of the screen.
I tried using Activity to set this particular screen and it worked fine(both fixed area at top and tabs are displayed correctly) but I need it using fragments as I am using Navigation Drawer so this has to be fragment. Please advice.
Modified xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCF8FB" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical" >
<TextView
android:id="@+id/loc_playlist_locname"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
<FrameLayout
android:id="@+id/playlist_realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
I made a few changes to your code: - In implementation, use the FragmentTabHost specified in layout - Use Framelayout specified within FragmentTabHost Element in layout in TabHost setup method. It seems to work, check out code below:
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.05"
android:visibility="invisible" />
<TextView
android:id="@+id/list_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textStyle="bold" />
<TextView
android:id="@+id/list_spec"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:visibility="invisible" />
<TextView
android:id="@+id/list_user"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCF8FB" >
<FrameLayout
android:id="@+id/list_realtabcontent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.95" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Fragment Class:
public class MyListFragment extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.activity_main, container,
false);
TextView name = (TextView) rootView.findViewById(R.id.list_name);
TextView spec = (TextView) rootView.findViewById(R.id.list_spec);
TextView users = (TextView) rootView.findViewById(R.id.list_user);
mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.id.list_realtabcontent);
Bundle arg1 = new Bundle();
arg1.putInt("Tab1", 1);
mTabHost.addTab(
mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
getResources().getDrawable(R.drawable.tab_chat_unselected)),
TabFragment.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Tab2", 2);
mTabHost.addTab(
mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
getResources().getDrawable(R.drawable.tab_chat_unselected)),
TabFragment.class, arg2);
Bundle arg3 = new Bundle();
arg3.putInt("Tab3", 3);
mTabHost.addTab(
mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
getResources().getDrawable(R.drawable.tab_chat_unselected)),
TabFragment.class, arg3);
return rootView;
}
这篇关于Android的错误:没有标签的空标签知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!