如何在Android Studio中解决此文本视图错误?

发布于 2025-01-27 05:50:20 字数 7168 浏览 1 评论 0原文

我使用Android Studio和Kotlin创建了一个应用程序。当我想更改应用程序的主题或更改应用程序的语言时,都会导致该应用程序崩溃,这两个错误都需要重新启动仪表板活动。我不知道如何解决:

这里是代码:

1-在dashboard.kt中:

class DashboardActivity : AppCompatActivity() {
private lateinit var storageReference: StorageReference
private lateinit var databaseReference: DatabaseReference
private lateinit var user: User
private lateinit var uid: String
private var toolbar: Toolbar? = null
private var viewPager: ViewPager? = null
private var tabLayout: TabLayout? = null
private var exploreFragment: ExploreFragment? = null
private var flightsFragment: FlightsFragment? = null
private var travelFragment: TravelFragment? = null
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    auth = FirebaseAuth.getInstance()
    uid = auth.currentUser?.uid.toString()
    databaseReference = FirebaseDatabase.getInstance().getReference("Users")
    if (uid.isNotEmpty()){
        getUserData()
    }
private fun getUserData() {
    val scoresRef = Firebase.database.getReference("Users")
    scoresRef.child(uid).addValueEventListener(object : ValueEventListener {
        @SuppressLint("SetTextI18n")
        override fun onDataChange(snapshot: DataSnapshot) {
            user = snapshot.getValue(User::class.java)!!
            val name = findViewById<TextView>(R.id.tvFirstNameAndLastName)
            val phoneNumber = findViewById<TextView>(R.id.tvPhoneNumber)
            name.text = user.name+" "+user.lastName
            phoneNumber.text = user.phoneNumber
            getUserProfile()
        }
        override fun onCancelled(error: DatabaseError) {
            finish()
        }
    })
}

通过删除这两行:

name.text = user.name+" "+user.lastName
        phoneNumber.text = user.phoneNumber

一切正常,我在两行上使用了这两行,从firebase检索数据,当我向他们展示时,它可以正常工作在导航视图中的NAV标题中,但是在更改应用程序主题/语言应用程序崩溃和Android Studio中显示了LogCat中的此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at af.azdreams.myconquer.DashboardActivity$getUserData$1.onDataChange(DashboardActivity.kt:293)

Dashboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/Orange"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <ImageButton
                android:id="@+id/btnSearchUsers"
                android:layout_width="wrap_content"
                android:background="#0000"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/icon_search"
                android:contentDescription="@string/todo" />
        </androidx.appcompat.widget.Toolbar>
        <com.google.android.material.tabs.TabLayout
            app:tabTextAppearance="@style/MineCustomTabText"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:itemTextAppearance="@style/menu"
    app:menu="@menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

Heaader.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="150dp"
android:id="@+id/header"
android:background="@color/Orange">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/circleImageView"
    android:layout_width="67dp"
    android:layout_height="67dp"
    android:layout_marginStart="5dp"
    app:civ_border_width="1dp"
    android:src="@drawable/profile"
    app:civ_border_color="@color/White"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.047"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.213" />

<TextView
    android:id="@+id/tvFirstNameAndLastName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="6dp"
    android:layout_marginTop="13dp"
    android:text=""
    android:textColor="@color/White"
    android:textSize="15dp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.05"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/circleImageView" />

<TextView
    android:id="@+id/tvPhoneNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="6dp"
    android:text=""
    android:textColor="@color/Black"
    android:textSize="13dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.05"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvFirstNameAndLastName"
    app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

当我在Google上搜索此错误时,人们也遇到了相同的问题,我尝试了Thier thier thier但是应用程序也崩溃了:(

如果您知道正确的解决方案,请帮助我。

感谢谁在帮助我。

I created an app using android studio and Kotlin. there is an error that cause that app crash when I want to change the theme of the app or changing language of the app that both needs to restart the dashboard activity. I do not know how to solve that :

here are the codes :

1- in dashboard.kt :

class DashboardActivity : AppCompatActivity() {
private lateinit var storageReference: StorageReference
private lateinit var databaseReference: DatabaseReference
private lateinit var user: User
private lateinit var uid: String
private var toolbar: Toolbar? = null
private var viewPager: ViewPager? = null
private var tabLayout: TabLayout? = null
private var exploreFragment: ExploreFragment? = null
private var flightsFragment: FlightsFragment? = null
private var travelFragment: TravelFragment? = null
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    auth = FirebaseAuth.getInstance()
    uid = auth.currentUser?.uid.toString()
    databaseReference = FirebaseDatabase.getInstance().getReference("Users")
    if (uid.isNotEmpty()){
        getUserData()
    }
private fun getUserData() {
    val scoresRef = Firebase.database.getReference("Users")
    scoresRef.child(uid).addValueEventListener(object : ValueEventListener {
        @SuppressLint("SetTextI18n")
        override fun onDataChange(snapshot: DataSnapshot) {
            user = snapshot.getValue(User::class.java)!!
            val name = findViewById<TextView>(R.id.tvFirstNameAndLastName)
            val phoneNumber = findViewById<TextView>(R.id.tvPhoneNumber)
            name.text = user.name+" "+user.lastName
            phoneNumber.text = user.phoneNumber
            getUserProfile()
        }
        override fun onCancelled(error: DatabaseError) {
            finish()
        }
    })
}

by removing these two lines :

name.text = user.name+" "+user.lastName
        phoneNumber.text = user.phoneNumber

everything works fine, I used that above two lines for retriving data from firebase and it works fine when I show them in nav header in navigationView, but while changing the app theme/language app crash and in android studio shows this error in logcat :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at af.azdreams.myconquer.DashboardActivity$getUserData$1.onDataChange(DashboardActivity.kt:293)

the dashboard.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/Orange"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <ImageButton
                android:id="@+id/btnSearchUsers"
                android:layout_width="wrap_content"
                android:background="#0000"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/icon_search"
                android:contentDescription="@string/todo" />
        </androidx.appcompat.widget.Toolbar>
        <com.google.android.material.tabs.TabLayout
            app:tabTextAppearance="@style/MineCustomTabText"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:itemTextAppearance="@style/menu"
    app:menu="@menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

and the heaader.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="150dp"
android:id="@+id/header"
android:background="@color/Orange">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/circleImageView"
    android:layout_width="67dp"
    android:layout_height="67dp"
    android:layout_marginStart="5dp"
    app:civ_border_width="1dp"
    android:src="@drawable/profile"
    app:civ_border_color="@color/White"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.047"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.213" />

<TextView
    android:id="@+id/tvFirstNameAndLastName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="6dp"
    android:layout_marginTop="13dp"
    android:text=""
    android:textColor="@color/White"
    android:textSize="15dp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.05"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/circleImageView" />

<TextView
    android:id="@+id/tvPhoneNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="6dp"
    android:text=""
    android:textColor="@color/Black"
    android:textSize="13dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.05"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvFirstNameAndLastName"
    app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

people had same problem when I googled this error, I tried thier ways but app also crashed:(

if you know the right solution for this, please help me.

thanks to who is helping me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2025-02-03 05:50:20

您尚未将任何布局设置为活动的内容视图,在ongreate方法中。

通过调用setContentView将布局设置为您的活动,

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

以访问navigationView's header,您首先需要获得其> Headerview然后您可以通过使用findViewById获得视图内部

val navigationView = findViewById<NavigationView>(R.id.navView)
val header: View = navigationView.getHeaderView(0)
val name = header.findViewById<TextView>(R.id.tvFirstNameAndLastName)

You haven't set any layout to activity's content view, in the onCreate method.

Set the layout to your activity by calling setContentView

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

To access a view inside the navigationView's header, you first need to get its headerView then you can get views inside it by using findViewById

val navigationView = findViewById<NavigationView>(R.id.navView)
val header: View = navigationView.getHeaderView(0)
val name = header.findViewById<TextView>(R.id.tvFirstNameAndLastName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文