E/ModuleIdSetter: exception when setting module id. What does this error mean? - android-studio

I was building an app on android studio and encountered this error E/ModuleIdSetter: exception when setting module id. I have no idea what this error is about and I would like to know what it is and how to resolve it. The full error is below.
2020-07-21 16:07:06.626 28147-28311/? E/ModuleIdSetter: exception when setting module id
java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms#202414040#20.24.14 (120700-319035315):2)
at aeua.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):4)
at aeud.b(:com.google.android.gms#202414040#20.24.14 (120700-319035315):9)
at aeql.a(Unknown Source:0)
at rnb.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):0)
at rjk.c(:com.google.android.gms#202414040#20.24.14 (120700-319035315):1)
at rji.b(:com.google.android.gms#202414040#20.24.14 (120700-319035315):1)
at rlz.b(:com.google.android.gms#202414040#20.24.14 (120700-319035315):6)
at rlz.c(:com.google.android.gms#202414040#20.24.14 (120700-319035315):6)
at rlz.b(:com.google.android.gms#202414040#20.24.14 (120700-319035315):10)
at rlz.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):17)
at rlz.g(:com.google.android.gms#202414040#20.24.14 (120700-319035315):3)
at sbg.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):2)
at sag.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):10)
at rzx.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):0)
at saa.handleMessage(:com.google.android.gms#202414040#20.24.14 (120700-319035315):28)
at android.os.Handler.dispatchMessage(Handler.java:107)
at aeiw.a(:com.google.android.gms#202414040#20.24.14 (120700-319035315):2)
at aeiw.dispatchMessage(:com.google.android.gms#202414040#20.24.14 (120700-319035315):14)
at android.os.Looper.loop(Looper.java:214)
at android.os.HandlerThread.run(HandlerThread.java:67)

System services not available to Activities before onCreate().
For example
This will throw an exception
private var audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
override fun onCreate(savedInstanceState: Bundle?) {
}
But, this won't
private lateinit var audioManager: AudioManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
}

Related

lateinit property mAdapter has not been initialized. How to solve this problem?

MainActivity.kt
class MainActivity : AppCompatActivity(), NewsItemclicked {
private lateinit var mAdapter: NewsListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
fetchData()
val adapter = NewsListAdapter( this)
recyclerView.adapter = mAdapter
}
private fun fetchData(){
val url = "https://saurav.tech/NewsAPI/top-headlines/category/sports/in.json"
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,
url,
null,
Response.Listener {
val newsJsonArray = it.getJSONArray("articles")
val newsArray = ArrayList<News>()
for(i in 0 until newsJsonArray.length()){
val newsJsonObject = newsJsonArray.getJSONObject(i)
val news = News(
newsJsonObject.getString("title"),
newsJsonObject.getString("author"),
newsJsonObject.getString("url"),
newsJsonObject.getString("urlToImage")
)
newsArray.add(news)
}
mAdapter.updateNews(newsArray)
},
Response.ErrorListener{
}
)
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
}
override fun onItemClicked(item: News) {
}
}
On the above MainActivity.kt of a News app is given.When I try to run the app the app is crashing. It is showing that lateinit property mAdapter has not been initialized. Please help me to figure out the problem.Please try to explain the simplest way as I am a beginner to Android so it is quite difficult to me to understand it quickly.
Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newstoday, PID: 10633
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newstoday/com.example.newstoday.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property mAdapter has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property mAdapter has not been initialized
It seems like you're instantiating your adapter and store the reference in a local variable (adapter) instead of the global one (mAdapter) which you specifically created to update its data once you receive the response from the network request.
If you change your code to:
mAdapter = NewsListAdapter(this)
the crash would be solved.

Program crashes whenever it gets string data from an another activity

I was just making a simple program that sends string that's typed in an EditText into a TextView in another activity, I'm a beginner and was following a kotlin tutorial on the internet, the program would crash whenever it gets the string from the 1st activity, The code of the 1st activity looks like this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener {
val message: String? = findViewById<EditText>(R.id.sameer).text.toString()
val kash = Intent(this, kos::class.java)
intent.putExtra("sameer", message)
startActivity(kash)
}
}
}
and the code for the 2nd activity goes like this:
class kos: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.kos)
val kanye: Bundle? = intent.extras
val shaheer = kanye!!.getString("sameer")
Toast.makeText(this, shaheer , Toast.LENGTH_SHORT)
findViewById<TextView>(R.id.UserisHambola).text = shaheer
}
}
I've tried to check when does the error start to happen, it appears that it starts to happen after declaring variable "shaheer" in the 2nd activity, if I delete that code with all the other codes after it, the program doesn't crash, I don't know why does this crash happen, Thank you for your time <3
Try to replace kanye!!.getString("sameer") with kanye?.getStringExtra("sameer") ?: "no data". This way the program will not crash if no value is passed under "sameer" key and the default "no data" will be stored to val shaheer. If you see no data in the toast, you passed the variable between activities incorrectly.
You do this in MainActivity:
val kash = Intent(this, kos::class.java)
intent.putExtra("sameer", message)
startActivity(kash)
You put the extra with the key "sameer" into the Intent referenced by the variable intent. You probably want to put the extra into the Intent referenced by the variable kash. Try this instead:
val kash = Intent(this, kos::class.java)
kash.putExtra("sameer", message)
startActivity(kash)

How can I use navigationRailView together with the Navigation library? (Have a standard switching of fragments, display of all icons)

Rail menu with nav_graph added. All the icons are switched, but the fragments do not change.
P.S I know about the option with Selected, but I want everything to work cleanly and with the Navigation library
My code is now:
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(
R.id.nav_host_container
) as NavHostFragment
navController = navHostFragment.navController
// Setup navigationRailView
val navigationRailView = findViewById<NavigationRailView>(R.id.navigation_rail)
navigationRailView.setupWithNavController(navController)
}
}
Android Studio offers:
// private fun NavigationRailView.setupWithNavController(navController: NavController) {}

kotlin app crashes when going one to another activity

i have 3 activitiy when i start my app i can go 1 activity to second but i cant go third it crashes
this is code of my third( last activity )
class thirdActivity3 : AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var textView2: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_third3)
textView = findViewById(R.id.textView)
textView2 = findViewById(R.id.textView2)
val extras = intent.extras
if(extras != null){
textView.text = extras.getString("NAME") //getString("NAME","")
textView2.text = extras.getInt("AGE").toString() //getInt("AGE",0).toString()
}
}
}
and this is my second activity
class secondActivity2 : AppCompatActivity() {
private lateinit var inputTextAge:EditText
private lateinit var finishButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second2)
inputTextAge=findViewById(R.id.inputTextAge)
finishButton=findViewById(R.id.finishButton)
val extras = intent.extras
var name = ""
if (extras != null){
name=extras.getString("PERSON_NAME","")
}
finishButton.setOnClickListener {
val age = finishButton.text.toString().toInt()
val intent = Intent(this,thirdActivity3::class.java)
intent.putExtra("NAME",name)
intent.putExtra("AGE",age)
startActivity(intent)
}
}
}
i am trying to make wizard for learning but my code have some problems. i have 3 activitys in kotling but when i go second activity to third it's stopping but its workd when i going first to second
2020-11-27 18:50:41.472 19343-19343/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 19343
java.lang.NumberFormatException: For input string: "Finish"
at java.lang.Integer.parseInt(Integer.java:615)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.myapplication.secondActivity2$onCreate$1.onClick(secondActivity2.kt:33)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
this is from logcat
Looking at the error message, it looks like your problem is this line:
val age = finishButton.text.toString().toInt()
The error is saying that you're trying to parse the string "Finish" as a number. It makes sense from this line that you're doing that. You have a button named "Finish". You get the text from the button and then call toInt() on that text, and you get this error.
What do you expect this line to do? Why do you expect querying a button's text to return an integer that represents age?

Crash on super.onCreate(savedInstanceState) after recreate activity

I have two activities in my project ActivityOne and MainActivity.
I start MainActivity using intent from ActivityOne then I toggle dark mode switch in the MainActivity and call recreate() on the activity.
but MainActivity crashes on super.onCreate(savedInstanceState) line when it recreated.
androidx fragment version: 1.2.5
lifecycle version: 2.2.0
MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
val userInterfaceMode = when (UIMode.currentUIMode) {
is UIMode.DarkMode -> AppCompatDelegate.MODE_NIGHT_YES
is UIMode.LightMode -> AppCompatDelegate.MODE_NIGHT_NO
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
AppCompatDelegate.setDefaultNightMode(userInterfaceMode)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
overridePendingTransition(
R.anim.enter_activity_fade_in_anim,
R.anim.enter_activity_fade_out_anim
)
if (savedInstanceState == null){
setupNavigation()
}
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
setupNavigation()
}
private fun setupNavigation(){
if (supportFragmentManager.findFragmentByTag(NAV_HOST_TAG) != null){
val host = supportFragmentManager
.findFragmentByTag(NAV_HOST_TAG) as NavHostFragment
supportFragmentManager
.beginTransaction()
.attach(host)
.setPrimaryNavigationFragment(host)
.commit()
}else{
val host = NavHostFragment.create(R.navigation.navigation_graph)
supportFragmentManager
.beginTransaction()
.add(
R.id.mainActivityRoot, host,
NAV_HOST_TAG
)
.setPrimaryNavigationFragment(host)
.commit()
}
}
Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.pitok.goodnews, PID: 6173
java.lang.RuntimeException: Unable to start activity ComponentInfo{MainActivity}: java.lang.IllegalArgumentException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4886)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4795)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:55)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:4845)
at android.app.ActivityThread.access$3300(ActivityThread.java:201)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException
at androidx.lifecycle.LifecycleRegistry.downEvent(LifecycleRegistry.java:263)
at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:314)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:334)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:118)
at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.java:150)
at androidx.navigation.NavBackStackEntry.setMaxLifecycle(NavBackStackEntry.java:130)
at androidx.navigation.NavController.popBackStackInternal(NavController.java:325)
at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:426)
at androidx.navigation.NavController.onGraphCreated(NavController.java:636)
at androidx.navigation.NavController.setGraph(NavController.java:586)
at androidx.navigation.NavController.setGraph(NavController.java:551)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:247)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2685)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2637)
at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2583)
at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:236)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:315)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
at me.pitok.goodnews.appMain.MainActivity.onCreate(MainActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119) 
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4886) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4795) 
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:55) 
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:4845) 
at android.app.ActivityThread.access$3300(ActivityThread.java:201) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:201) 
at android.app.ActivityThread.main(ActivityThread.java:6864) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
Seems to be an issue using the same id for the navigation graph and the destination.
https://issuetracker.google.com/issues/161825212
We had a very similar crash and solved it by using a unique id for the navigation graph.

Resources