android java.lang.NullPointerException:findViewById(R.id.addtoUserFriendList)不能为null

ut6juiuv  于 2023-04-28  发布在  Android
关注(0)|答案(2)|浏览(84)

你好,我得到这样一个错误,我无法找到解决方案的错误;
java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.chatapp/com.example.chatapp.MainActivity}:java.lang.NullPointerException:findViewById(R.id.addtoUserFriendList)不能为null
我的代码是:

private lateinit var userRecyclerView: RecyclerView

private lateinit var userList: ArrayList<User>
    
private lateinit var adapter: UserAdapter
  
 private lateinit var mAuth: FirebaseAuth
    
private lateinit var addFriend: Button
  
  private lateinit var mDbRef: DatabaseReference
   
 var actionBarDrawerToggle: ActionBarDrawerToggle? = null
   
 private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_main)

        mAuth = FirebaseAuth.getInstance()
        mDbRef = FirebaseDatabase.getInstance().getReference()

        userList = ArrayList()
        adapter = UserAdapter(this, userList)

        userRecyclerView = findViewById(R.id.userRecyclerview)

        userRecyclerView.layoutManager = LinearLayoutManager(this)
        userRecyclerView.adapter = adapter

            mDbRef.child("user").addValueEventListener(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {

                    userList.clear()
                    for (postSnapshot in snapshot.children) {
                        val user = postSnapshot.getValue(User::class.java)

                        if (mAuth.currentUser?.uid != user?.uid) {
                            userList.add(user!!)
                        }
                    }

                    adapter.notifyDataSetChanged()
                }

                override fun onCancelled(error: DatabaseError) {

                }

            })

        addFriend = findViewById(R.id.addtoUserFriendList)

        addFriend.setOnClickListener {
            Toast.makeText(this@MainActivity, "Başarılı", Toast.LENGTH_SHORT).show()
            var friend = Friend(name = "Merhaba", "merhaba@gmail.com")
            saveFriend(friend)
        }
    }
`

布置代号:

<?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="wrap_content"
    android:visibility="visible">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/user_image"
            android:layout_width="39dp"
            android:layout_height="36dp"
            android:src="@drawable/profile"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="4dp"
            tools:layout_editor_absoluteY="3dp" />

        <Button
            android:id="@+id/addtoUserFriendList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Arkadaş Olarak Ekle"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="244dp"
            tools:layout_editor_absoluteY="16dp" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="5dp"
        android:text="Hasan"
        android:textSize="24sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/txt_name"
        android:layout_marginTop="5dp"
        android:background="@color/black" />


</RelativeLayout>

我试过了,但我解不出来

mrfwxfqh

mrfwxfqh1#

似乎addFriend按钮在另一个XML文件中,因为在您附加的文件中没有这样的视图。
您必须在activity_main文件中声明addFriend button,以便在Activity中的代码中使用它。

mo49yndu

mo49yndu2#

你定义了一个非空变量private lateinit var addFriend: Button,并传递了一个空值。这就是你得到空指针异常的原因。为了修复,让变量像private lateinit var addFriend: Button?一样可以为空,或者只是传递一个非空值给它。

相关问题