简体   繁体   English

在 Android Studio 中将底部导航栏与其他代码合并

[英]Merging a Bottom Nav bar with other code in Android Studio

I'm building an app with multiple people right now and I'm trying to merge the bottom navigation bar I made with someone else's code - but the app crashes immediately due to a runtime error after I implemented my navigation bar with this really long message.我现在正在与多人一起构建一个应用程序,并且我正在尝试将我制作的底部导航栏与其他人的代码合并 - 但是在我使用这条非常长的消息实现导航栏后,由于运行时错误,应用程序立即崩溃. Anyone know how to fix this?有人知道怎么修这个东西吗?

2020-06-30 00:59:26.429 7505-7505/com.example.unieats E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.unieats, PID: 7505
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unieats/com.example.unieats.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
        at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:103)
        at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233)
        at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:227)
        at androidx.navigation.ui.ActivityKt.setupActionBarWithNavController(Activity.kt:74)
        at com.example.unieats.MainActivity.onCreate(MainActivity.kt:27)
        at android.app.Activity.performCreate(Activity.java:7458)
        at android.app.Activity.performCreate(Activity.java:7448)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

Update: it seems like my MainActivity file is the one causing the problems.更新:似乎我的 MainActivity 文件是导致问题的文件。 Below this is the code for that file - everything before val navView is code that already existed, and everything after is what I added.下面是该文件的代码 - val navView 之前的所有内容都是已经存在的代码,之后的所有内容都是我添加的。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        val navController = findNavController(R.id.nav_host_fragment)
        val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_search, R.id.navigation_log,
            R.id.navigation_settings, R.id.navigation_profile))
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController) 
         
    }
}

Can you share the code before before val navView i think there is something wrong there or check your AppTheme in your res/values/style.xml if your AppTheme parent ends with.NoActionBar if so change it to.DarkActionBar Hope it helps你能在 val navView 之前分享代码吗?我认为那里有问题,或者如果你的 AppTheme 父级以.NoActionBar 结尾,请将其更改为.DarkActionBar 希望它有帮助

The error doesn't seem to be coming from the navigation bar that you are merging, but instead from the ActionBar.该错误似乎不是来自您正在合并的导航栏,而是来自 ActionBar。 Make sure you have the action bar you are calling in the activity/fragment created so that you can set the title to it, as the error indicates.确保您在创建的活动/片段中调用了操作栏,以便您可以为其设置标题,如错误所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM