java.lang.RuntimeException:无法启动活动 ComponentInfo{com.amco.scanai/com.amco.scanai.activities.profile.ProfileActivity}:java.lang.IllegalStateException:TabLayoutMediator 在 ViewPager2 在 android.app.ActivityThread 有适配器之前附加android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 上 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 上 android.app.servertransaction.TransactionExecutor 上的 .performLaunchActivity(ActivityThread.java:3114)。 executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 在 android.os.Handler.dispatchMessage (Handler.java:106) 在 android.app 的 android.os.Looper.loop(Looper.java:214)。ActivityThread.main(ActivityThread.java:7050) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 在 com.android 的 java.lang.reflect.Method.invoke(Native Method)。 internal.os.ZygoteInit.main(ZygoteInit.java:965) 原因:java.lang.IllegalStateException: TabLayoutMediator 在 ViewPager2 之前附加在 com.google.android.material.tabs.TabLayoutMediator.attach(TabLayoutMediator.java:117 ) 在 com.amco.scanai.utilities.BindingAdapters.setPager2(BindingAdapters.java:163) 在 com.amco.scanai.databinding.ActivityProfileBindingImpl.executeBindings(ActivityProfileBindingImpl.java:207) 在 androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding. java:473) 在 com.amco.scanai.mvvm.base.BaseActivity 的 androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:445)。performDataBinding(BaseActivity.java:105) 在 com.amco.scanai.mvvm.base.BaseActivity.onCreate(BaseActivity.java:60) 在 com.amco.scanai.activities.profile.ProfileActivity.onCreate(ProfileActivity.java:25)在 android.app.Activity.performCreate(Activity.java:7327) 在 android.app.Activity.performCreate(Activity.java:7318) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在 android.app。 ActivityThread.performLaunchActivity(ActivityThread.java:3094) 在 android.app.servertransaction.TransactionExecutor 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) .executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app。ActivityThread$H.handleMessage(ActivityThread.java:1948) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main (ActivityThread.java:7050)
我将 ViewPager2 与 Tab 布局一起使用,通过使用绑定适配器我试图将适配器绑定到 ViewPager,然后将 ViewPager 绑定到 Tab 布局。但它不断给出上述错误。以下是代码。
Profile_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="profile"
type="com.amco.scanai.activities.profile.ProfileViewModel" />
</data>
<RelativeLayout 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="@color/white"
android:orientation="vertical"
tools:context=".activities.profile.ProfileActivity">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="@dimen/_10sdp">
<include
layout="@layout/view_button_back"
android:layout_width="@dimen/_30sdp"
android:layout_height="@dimen/_30sdp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:viewModel="@{(com.amco.scanai.mvvm.base.BaseViewModel)profile}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginHorizontal="@dimen/_7sdp"
android:layout_toEndOf="@id/btnBack"
android:fontFamily="@font/titillium_web_regular"
android:text="My Profile"
android:textColor="@color/black"
android:textSize="@dimen/_16ssp" />
</RelativeLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/_40sdp"
android:layout_below="@id/header"
android:layout_centerHorizontal="true"
android:background="@color/white"
app:pager2="@{ viewPager }"
app:tabBackground="@color/white"
app:tabGravity="fill"
app:tabIndicatorColor="@color/selected_tab"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="@dimen/_4sdp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/selected_tab"
app:tabTextAppearance="@style/tab"
app:tabTextColor="@color/unselected_tab" />
<View
android:id="@+id/div1"
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:layout_below="@id/tab_layout"
android:layout_marginHorizontal="@dimen/_20sdp"
android:layout_marginVertical="@dimen/_10sdp"
android:background="#EFEFEF" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/div1"
adapter2="@{profile.adapter}"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:onPageChangeListener="@{profile.onPageChangeListener}"
app:setCurrentItem="@{profile.currentPosition}" />
</RelativeLayout>
</layout>
ProfileViewModel.java
public class ProfileViewModel extends BaseViewModel<ProfileNavigator> {
private static final String TAG = "ScanningViewModel";
private int currentPosition;
private Boolean initializeTabLayout;
private SectionsPagerAdapter adapter;
public ProfileViewModel(DataManager dataManager, ResourceProvider resourceProvider, SchedulerProvider schedulerProvider) {
super(dataManager, resourceProvider, schedulerProvider);
setCurrentPosition(0);
setInitializeTabLayout(false);
}
public void setActivity(ProfileActivity activity) {
setAdapter(new SectionsPagerAdapter(activity));
setInitializeTabLayout(true);
}
@Bindable
public Boolean getInitializeTabLayout() {
return initializeTabLayout;
}
public void setInitializeTabLayout(Boolean initializeTabLayout) {
this.initializeTabLayout = initializeTabLayout;
notifyPropertyChanged(BR.initializeTabLayout);
}
@Bindable
public int getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
notifyPropertyChanged(BR.currentPosition);
}
@Bindable
public ViewPager2.OnPageChangeCallback getOnPageChangeListener() {
return new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
setCurrentPosition(position);
}
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
};
}
@Bindable
public SectionsPagerAdapter getAdapter() {
return adapter;
}
public void setAdapter(SectionsPagerAdapter adapter) {
this.adapter = adapter;
notifyPropertyChanged(BR.adapter);
}
@Override
public void goBack() {
getNavigator().goBack();
}
@Override
public void goNext() {
}
public void onBackPressed() {
if (getCurrentPosition() == 0) {
getNavigator().goBack();
} else {
setCurrentPosition(0);
}
}
public static class SectionsPagerAdapter extends FragmentStateAdapter {
ProfileActivity activity;
public SectionsPagerAdapter(FragmentActivity fa) {
super(fa);
activity = (ProfileActivity) fa;
}
@NotNull
@Override
public Fragment createFragment(int position) {
return position == 0 ? new PersonalFragment(new WeakReference<>(activity)) :
position == 1 ? new PasswordFragment(new WeakReference<>(activity)) :
new BusinessFragment(new WeakReference<>(activity));
}
@Override
public int getItemCount() {
return 3;
}
}
}
BindingAdapters.java
@BindingAdapter("pager2")
public static void setPager2(TabLayout tabLayout, ViewPager2 viewPager) {
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> tab.setText(position == 0 ? "Personal" : position == 1 ? "Password" : "Business")
).attach();
}
@BindingAdapter("adapter2")
public static void setAdapter2(ViewPager2 viewPager, ProfileViewModel.SectionsPagerAdapter adapter) {
viewPager.setAdapter(adapter);
}
@BindingAdapter("onPageChangeListener")
public static void setonPageChangeListener(ViewPager2 viewPager2, ViewPager2.OnPageChangeCallback listener) {
if (listener != null)
viewPager2.registerOnPageChangeCallback(listener);
}