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

问题描述 投票:0回答:1

我有一个试图从我创建的库/模块中打开主要活动的应用程序,但出现以下错误:

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 Navigator(){

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

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

我在模块内部的活动如下:

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>

并且我在应用程序上这样调用新活动(DrawerActivity):

Navigator.newInstance().navigateToMainActivity(this)

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

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

感谢大家的帮助:)

更新:

此错误是由于模块的布局与应用程序的布局具有相同的名称。一旦我更改了名称,一切都会正常进行。

android kotlin module aar
1个回答
0
投票

此错误是由于模块的布局与应用程序的布局具有相同的名称。一旦我更改了名称,一切都会正常进行。

© www.soinside.com 2019 - 2024. All rights reserved.