Android - 从外部应用程序模块启动 activity 时获取空指针

Android - Getting null pointer when starting activity from external app module

我有一个应用程序试图从我创建的 library/module 打开主 activity,但出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.IllegalStateException: findViewById(R.id.toolbar) must not be null

要导入模块,我创建了一个新模块,选择了 .aar 文件,在 gradle 中添加了依赖项 作为

implementation project(path: ':test-module')

在我的模块项目中,我使用 apply plugin: 'com.android.library' 并删除了 gradle 文件中的应用程序 ID。

我实现了一个 class 来处理到模块内部 activity 的导航:

class Navigator(){

    fun navigateToMainActivity(context: Context){
        val intent  = Intent(context,DrawerActivity::class.java)
        context.startActivity(intent)
    }

    companion object{
        fun newInstance(): Navigator = Navigator()
    }
}

我的 activity 模块内部看起来像这样:

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()
}

}

模块的清单是:

<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>

然后我在我的应用程序中这样调用新的 activity (DrawerActivity):

Navigator.newInstance().navigateToMainActivity(this)

我也尝试通过在应用程序上创建意图并使用包管理器或 Intent(this, DrawerActivity::class.java) 启动它来调用 activity 作为意图,但我仍然遇到相同的错误

java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.IllegalStateException: findViewById(R.id.toolbar) must not be null

提前感谢大家的帮助:)

更新:

此错误是由于我的模块布局与我的应用程序布局同名。一旦我更改了名称,一切都完美无缺。

此错误是由于我的模块布局与我的应用布局同名。一旦我更改了名称,一切都完美无缺。