Android 活动无法启动:ID未引用此活动内的视图

Android 活动无法启动:ID未引用此活动内的视图,android,kotlin,Android,Kotlin,我试图从导航中导航到activity\u profile,但它崩溃了,我在catlog中得到一个错误。我检查了我的代码是否有拼写错误或错误,但我一直遇到以下错误消息: 2021-03-21 22:25:26.299 16411-16411/com.example.kumoapp2 D/Testing navProfile: profile 2021-03-21 22:25:26.300 1880-2012/system_process I/ActivityManager: START u0 {c

我试图从导航中导航到
activity\u profile
,但它崩溃了,我在catlog中得到一个错误。我检查了我的代码是否有拼写错误或错误,但我一直遇到以下错误消息:

2021-03-21 22:25:26.299 16411-16411/com.example.kumoapp2 D/Testing navProfile: profile
2021-03-21 22:25:26.300 1880-2012/system_process I/ActivityManager: START u0 {cmp=com.example.kumoapp2/.Profile} from uid 10086
2021-03-21 22:25:26.310 1594-6140/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 7277303 , only wrote 7277040
2021-03-21 22:25:26.444 16411-16411/com.example.kumoapp2 W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@f0b0b42
2021-03-21 22:25:26.540 1880-1951/system_process I/GnssLocationProvider: WakeLock acquired by sendMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@da59bf4)
2021-03-21 22:25:26.542 1880-1905/system_process I/GnssLocationProvider: WakeLock released by handleMessage(REPORT_SV_STATUS, 0, com.android.server.location.GnssLocationProvider$SvStatusInfo@da59bf4)
2021-03-21 22:25:26.717 16411-16411/com.example.kumoapp2 D/AndroidRuntime: Shutting down VM
2021-03-21 22:25:26.724 16411-16411/com.example.kumoapp2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kumoapp2, PID: 16411
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kumoapp2/com.example.kumoapp2.Profile}: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        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:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
        at android.app.Activity.requireViewById(Activity.java:2678)
        at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:363)
        at androidx.navigation.Navigation.findNavController(Navigation.java:58)
        at androidx.navigation.ActivityKt.findNavController(Activity.kt:30)
        at com.example.kumoapp2.Profile.onCreate(Profile.kt:36)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        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:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2021-03-21 22:25:26.731 1880-2012/system_process W/ActivityManager:   Force finishing activity com.example.kumoapp2/.Profile
2021-03-21 22:25:26.743 1880-2012/system_process W/ActivityManager:   Force finishing activity com.example.kumoapp2/.MainActivity
2021-03-21 22:25:26.766 1880-1906/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver
2021-03-21 22:25:26.766 1880-1906/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.chimera.GmsIntentOperationService$PersistentTrustedReceiver
2021-03-21 22:25:26.770 16411-16411/com.example.kumoapp2 I/Process: Sending signal. PID: 16411 SIG: 9
2021-03-21 22:25:26.859 1880-1971/system_process W/InputDispatcher: channel '8a946ff com.example.kumoapp2/com.example.kumoapp2.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2021-03-21 22:25:26.862 1705-1705/? I/Zygote: Process 16411 exited due to signal (9)
2021-03-21 22:25:26.863 1880-1971/system_process E/InputDispatcher: channel '8a946ff com.example.kumoapp2/com.example.kumoapp2.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2021-03-21 22:25:26.868 1880-2012/system_process I/ActivityManager: Process com.example.kumoapp2 (pid 16411) has died: vis  +99TOP 
2021-03-21 22:25:26.869 1880-1909/system_process W/libprocessgroup: kill(-16411, 9) failed: No such process
2021-03-21 22:25:26.873 1880-1909/system_process I/libprocessgroup: Successfully killed process cgroup uid 10086 pid 16411 in 4ms
2021-03-21 22:25:26.888 1880-6062/system_process I/WindowManager: WIN DEATH: Window{8a946ff u0 com.example.kumoapp2/com.example.kumoapp2.MainActivity}
2021-03-21 22:25:26.888 1880-6062/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '8a946ff com.example.kumoapp2/com.example.kumoapp2.MainActivity (server)'
这是我的个人资料。kt

package com.example.kumoapp2

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import com.google.android.material.navigation.NavigationView
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_home.*

class Profile : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    private lateinit var appBarConfiguration: AppBarConfiguration
    private var drawerLayout: DrawerLayout? = null
    private var toolbar: Toolbar? = null
    private var navController: NavController? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)

        drawerLayout = findViewById(R.id.drawer_layout)
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_profile, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send
            ), drawerLayout
        )

        navController = findNavController(R.id.nav_host_fragment)

        val navView: NavigationView = findViewById(R.id.nav_view)
        navView.setNavigationItemSelectedListener(this)
        navView.bringToFront()

        Log.d("__PROFILE__", "onCreate")
    }

    override fun onNavigationItemSelected(item: MenuItem):Boolean {
        Log.d("onNavItemSelected", "onNavItemSelected")

        when (item.itemId) {
            R.id.nav_sign_out -> {
                Log.d("Testing logout ", "user")
                FirebaseAuth.getInstance().signOut()
                startActivity(Intent(this@Profile, LoginActivity::class.java))
                finish()
            }

            R.id.nav_profile -> {
                Log.d("Testing navProfile ", "profile")

                startActivity(Intent(this@Profile, Profile::class.java))
            }
        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }


}
这里是activity_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:showIn="@layout/activity_profile"
    tools:context=".Profile">

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="50dp"
        android:background="@color/colorPrimary" />

    <ScrollView
        android:id="@+id/scrolView_account"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <RelativeLayout
            android:id="@+id/linear_account"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="100dp"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="100dp"
                    android:orientation="vertical">

                        <EditText
                            android:id="@+id/plain_text_input"
                            android:layout_height="wrap_content"
                            android:layout_width="match_parent"
                            android:hint="Email Address"
                            android:imeOptions="actionNext"
                            android:inputType="textEmailAddress"
                            android:textColorHint="@color/colorPrimary" />

                </LinearLayout>
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/imageview_account_profile"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:src="@mipmap/ic_launcher"
                    app:civ_border_color="#FFFFFF"
                    app:civ_border_width="2dp" />

                <com.google.android.material.floatingactionbutton.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignEnd="@+id/imageview_account_profile"
                    android:layout_marginTop="10dp"
                    android:id="@+id/floatingActionButton" />

            </RelativeLayout>


        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

我试着看看是否有其他人也有同样的问题,但主要是关于
navController
setContentView()之后没有被调用。但是在我的例子中,我在
setContentView()之后调用它。是什么导致了这个问题

drawerLayout = findViewById(R.id.drawer_layout)

activity_profile.xml没有抽屉布局id,因此您的
@+id/nav_host_片段在
activity_profile.xml
中的哪里?我没有看到它。它应该在
android:id=“
”里面吗?你需要有一个
NavHostFragment
和这个id…是的。你认为
findNavController
会做什么?它属于
android:id=“”