Reputation: 1
In the following code I am getting the following errors: tabLayout <com.google.android.material.tabs.TabLayout>: No speakable text present Failed to instantiate one or more classes
I have looked online and all the similar issues pertain to edittext and not having a description in those fields, however my issue is with tablayout and I have no edittext in the code.
<androidx.coordinatorlayout.widget.CoordinatorLayout
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=".MainActivity">
<!-- TabLayout for Start, History, Settings -->
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="68dp"
app:tabIndicator="@drawable/tab_indicator"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicatorGravity="stretch"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/black" />
<!-- ViewPager2 for swiping between fragments -->
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_below="@id/tabLayout"
android:layout_gravity="fill"
android:layout_weight="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here is my main activity file
What I tried is listed above
package com.example.sahir_sood_myruns1
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
class MainActivity : AppCompatActivity() {
// Declare variables for TabLayout, ViewPager2, and the adapter
private lateinit var tabLayout: TabLayout
private lateinit var viewPager2: ViewPager2
private lateinit var adapter: FragmentPageAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Set the content view to your XML layout
// Find the TabLayout and ViewPager2 in the layout by their respective IDs
tabLayout = findViewById(R.id.tabLayout)
viewPager2 = findViewById(R.id.viewPager2)
// Initialize the adapter with the FragmentManager and the lifecycle
adapter = FragmentPageAdapter(supportFragmentManager, lifecycle)
// Add tabs to the TabLayout, each with a title (Start, History, Settings)
tabLayout.addTab(tabLayout.newTab().setText("Start").setContentDescription("Start Tab"))
tabLayout.addTab(tabLayout.newTab().setText("History").setContentDescription("History Tab"))
tabLayout.addTab(tabLayout.newTab().setText("Settings").setContentDescription("Settings Tab"))
// Set the adapter for the ViewPager2 so it knows which fragments to display
viewPager2.adapter = adapter
// Add a listener to handle user interaction with the TabLayout (i.e., when the user clicks on a tab)
tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
// This method is called when a tab is selected
override fun onTabSelected(tab: TabLayout.Tab?) {
if (tab != null) {
// When a tab is selected, update the ViewPager2 to show the corresponding fragment
viewPager2.currentItem = tab.position
}
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
// This method is called when a tab is unselected, but nothing happens in this case
}
override fun onTabReselected(tab: TabLayout.Tab?) {
// This method is called when a tab is reselected, but nothing happens in this case
}
})
// Add a callback to handle page changes in the ViewPager2 (i.e., when the user swipes between pages)
viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
// This method is called when a new page is selected
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
// When the page is changed, update the TabLayout to highlight the corresponding tab
tabLayout.selectTab(tabLayout.getTabAt(position))
}
})
}
}
Upvotes: 0
Views: 171