Issue
New to android and kotlin here. Making my first app and I'm trying to use the getPageTitle
function to give my tabs their titles (of which are string resources). The full implementation is as follows:
class FAAMainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar as Toolbar)
val pagerAdapter = SectionsPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
pager.adapter = pagerAdapter
tabs.setupWithViewPager(pager)
}
private class SectionsPagerAdapter : FragmentPagerAdapter{
constructor(fm: FragmentManager, behavior: Int) : super(fm, behavior)
override fun getItem(position: Int): Fragment {
when (position) {
0 -> return HomeFragment()
1 -> return KittensFragment()
2 -> return CatsFragment()
3 -> return FosterersFragment()
4 -> return FAAUsersFragment()
else -> {
return HomeFragment()
}
}
}
override fun getCount(): Int {
return 5
}
override fun getPageTitle(position: Int): CharSequence? {
when(position) {
0 -> Resources.getSystem().getText(R.string.home_tab)
1 -> Resources.getSystem().getText(R.string.kitten_tab)
2 -> Resources.getSystem().getText(R.string.cat_tab)
3 -> Resources.getSystem().getText(R.string.fosterer_tab)
4 -> Resources.getSystem().getText(R.string.faa_user_tab)
else -> "Error"
}
return "Error"
}
}
}
Trying to run the application gives the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.ac.aber.dcs.cs31620.faa/uk.ac.aber.dcs.cs31620.faa.ui.FAAMainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7f0d0027
I don't understand why it cannot find the String resource.
My strings.xml;
<resources>
<string name="app_name">Feline Adoption Agency</string>
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="home_tab">Home</string>
<string name="kitten_tab">Kittens</string>
<string name="cat_tab">Cats</string>
<string name="fosterer_tab">Fosterers</string>
<string name="faa_user_tab">FAA Users</string>
</resources>
More information as I am putting a bounty on this:
- I can verify the resources are being placed in the
app/build/generated/not_namespaced_r_class_sources/debug/r/uk/ac/aber/dcs/cs31620/faa/R.java
file correctly. - However they are not put into the
app/build/generated/not_namespaced_r_class_sources/debug/r/androidx/appcompat/R.java
and I am not sure that is the cause of the issue. My imports for the
FAAMainActivity
class are:import android.content.res.Resources import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter import kotlinx.android.synthetic.main.activity_main.* import uk.ac.aber.dcs.cs31620.faa.R import uk.ac.aber.dcs.cs31620.faa.ui.cats.CatsFragment import uk.ac.aber.dcs.cs31620.faa.ui.faa_users.FAAUsersFragment import uk.ac.aber.dcs.cs31620.faa.ui.fosterers.FosterersFragment import uk.ac.aber.dcs.cs31620.faa.ui.home.HomeFragment import uk.ac.aber.dcs.cs31620.faa.ui.kittens.KittensFragment
I've uploaded the project here if anyone want's to try it out.
Solution
Calling Resources.getSystem()
provides System level resources and not your
application level resources as per the doc:
Return a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based n orientation, etc).
You'll need application or activity level context in order to retrieve the strings from your strings.xml. I have changed your SectionsPagerAdapter
to this in order to fix your error:
private class SectionsPagerAdapter(fm: FragmentManager, behavior: Int, private val context: Context) :
FragmentPagerAdapter(fm, behavior) {
override fun getItem(position: Int): Fragment {
return when (position) {
0 -> HomeFragment()
1 -> KittensFragment()
2 -> CatsFragment()
3 -> FosterersFragment()
4 -> FAAUsersFragment()
else -> {
HomeFragment()
}
}
}
override fun getCount(): Int {
return 5
}
override fun getPageTitle(position: Int): CharSequence? {
return when(position) {
0 -> context.getString(R.string.kitten_tab)
1 -> context.getString(R.string.kitten_tab)
2 -> context.getString(R.string.cat_tab)
3 -> context.getString(R.string.fosterer_tab)
4 -> context.getString(R.string.faa_user_tab)
else -> context.getString(R.string.home_tab)
}
}
}
FAAMainActivity
val pagerAdapter = SectionsPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, this) // Passed "this" as context
Here, I have primarily changed following things in your code,
From
private class SectionsPagerAdapter : FragmentPagerAdapter{
constructor(fm: FragmentManager, behavior: Int) : super(fm, behavior)
...
}
To
private class SectionsPagerAdapter(fm: FragmentManager, behavior: Int, private val context: Context) :
FragmentPagerAdapter(fm, behavior) {
...
}
I have changed your JAVA style constructor to kotlin's primary constructor and added Context
as third parameter. Now we will use that Context
to get the string instead of Resources.getSystem()
.
Answered By - Sandip Fichadiya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.