I've got an application that tries to open the main activity from a library/module that I created and I get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.IllegalStateException: findViewById(R.id.toolbar) must not be null
To import the module I created a new module, selected the .aar file, added the dependency in gradle as
implementation project(path: ':test-module')
In the project of my module I am using apply plugin: 'com.android.library'
and deleted application id in gradle file.
I implemented a class that handles the navigation to the activity inside the module:
class Navigator(){
fun navigateToMainActivity(context: Context){
val intent = Intent(context,DrawerActivity::class.java)
context.startActivity(intent)
}
companion object{
fun newInstance(): Navigator = Navigator()
}
}
My activity inside the module looks like this:
class DrawerActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
the manifest of the module is:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shadows.mydrawer">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".DrawerActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.example.main.mainactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And I call the new activity (DrawerActivity) on my application like this:
Navigator.newInstance().navigateToMainActivity(this)
I´ve also tried calling the activity by creating an intent on application and starting it with package manager or with Intent(this, DrawerActivity::class.java)
as an intent but I am still getting the same error
java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.IllegalStateException: findViewById(R.id.toolbar) must not be null
Thanks everyone in advance for the help :)
Update:
This error was due to my module's layouts having the same name as my app's layouts. Once I changed the name, every thing worked perfectly.
This error was due to my module's layouts having the same name as my app's layouts. Once I changed the name, every thing worked perfectly.