0

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.

1 Answers1

0

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)
Praveen
  • 3,186
  • 2
  • 8
  • 23
  • it also not working and change nothing – days long coder May 08 '22 at 10:43
  • Have you checked your layout has any `textView` with `id` `tvFirstNameAndLastName`? – Praveen May 08 '22 at 10:48
  • I added related your wanted codes at the end the question please check. – days long coder May 08 '22 at 10:56
  • I want to tell you bellow thing that may help to guess the problem : [I created an activity with layout file wich is called DashboardActivity.kt and layout file is activity_dashboard.xml] [I created also a navigationView in activity_dashboard.xml] [and an header file called header.xml that contains textview with the id of tvFirstNameAndLastName and tvPhoneNumber id of another textView ] [I called header.xml layout into navigationView that is in activity_dashboard.xml] I think if I could find tvFirstNameAndLastName using (binding.tvFirstNameAndLastName.text) it will solve, but tvFNALN is red:( – days long coder May 08 '22 at 11:04
  • You can't directly access views of a header of a navigation view, I've updated my answer. – Praveen May 08 '22 at 11:47
  • the app not even open it crash at the opening time with new logcat error : [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:298)] – days long coder May 08 '22 at 12:08