当PagerAdapter调用notifyDataSetChanged()覆盖TabLayout类和Exception时,TabLayout将删除所有选项卡

时间:2016-10-17 19:20:25

标签: android android-tablayout

我以前在TabLayout中使用文字作为标题,但我决定在onCreateView使用tabLayout.getTabAt(0).setIcon(R.drawable.ic)时更改为图标。但是当我通过notifyDataSetChanged()在PagerArray中更新我的数据集时,所有选项卡都变为空。我找到了:Issue 202402

他们编写的解决方案是创建新的类扩展TabLayout。所以我做到了:

public class SuperTabLayout extends TabLayout {
public SuperTabLayout(Context context) {
    super(context);
}

@Override
public void addTab(Tab tab, boolean setSelected) {
    tab.setIcon(R.drawable.ic_assessment_24dp);
    super.addTab(tab, setSelected);
}

}

他们写作以免忘记布局

    <com.paragraf.workhours.SuperTabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tablay1"
    android:layout_below="@+id/ivPersonAvatar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    class="com.paragraf.workhours.SuperTabLayout"
   />

但我有这样的事情:

10-17 21:07:18.600 28964-28964/com.paragrafxv.workhours E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.paragrafxv.workhours, PID: 28964
                                                                      android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.paragrafxv.workhours.SuperTabLayout
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                          at com.paragrafxv.workhours.SitePersonFragment.onCreateView(SitePersonFragment.java:86)
                                                                          at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                          at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                          at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5539)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                       Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.paragrafxv.workhours.SuperTabLayout
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:628)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                          at com.paragrafxv.workhours.SitePersonFragment.onCreateView(SitePersonFragment.java:86) 
                                                                          at android.app.Fragment.performCreateView(Fragment.java:2220) 
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) 
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
                                                                          at android.app.BackStackRecord.run(BackStackRecord.java:793) 
                                                                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
                                                                          at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482) 
                                                                          at android.os.Handler.handleCallback(Handler.java:739) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5539) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                       Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                                                                          at java.lang.Class.getConstructor(Class.java:528)
                                                                          at java.lang.Class.getConstructor(Class.java:492)
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:592)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                          at com.paragrafxv.workhours.SitePersonFragment.onCreateView(SitePersonFragment.java:86) 
                                                                          at android.app.Fragment.performCreateView(Fragment.java:2220) 
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) 
                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 
                                                                          at android.app.BackStackRecord.run(BackStackRecord.java:793) 
                                                                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 
                                                                          at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482) 
                                                                          at android.os.Handler.handleCallback(Handler.java:739) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5539) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

有人?

2 个答案:

答案 0 :(得分:0)

您需要实现所有父类&#39;构造函数。具体做法是:

由于您没有声明第二个,因此无法从XML中使用dom-repeat

答案 1 :(得分:0)

在致电super.addTab(tab, setSelected)后,您是否故意致电tab.setIcon()?这可能是个问题。