无法投射工具栏

Toolbar cannot be cast

在我点击每个用户为每个用户打开消息 activity 后,我试图在聊天应用程序中制作,但我使用的是 androidx,工具栏无法转换为 android.widget.Toolbar.I只想显示带有 username.I 的文本 从我的 cardview.I 中获取 ID "username1" 在另一个 xml 中使用用户名 1 作为我所有用户的 class appear.Here 是我的数据库:

留言activity:

public class MessageActivity extends AppCompatActivity {
    TextView username;
    FirebaseUser fuser;
    DatabaseReference reference;
    Intent intent;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_message );
       TabLayout appBarLayout=findViewById ( R.id.tab_2);



            username=findViewById ( R.id.username1 );

            intent=getIntent ();
            String userid=intent.getStringExtra ( "userid" );
            fuser= FirebaseAuth.getInstance ().getCurrentUser ();
            reference= FirebaseDatabase.getInstance ().getReference ("users").child ( "FIRST NAME");
            reference.addValueEventListener ( new ValueEventListener () {
                @Override
                public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
                    User user=dataSnapshot.getValue (User.class);
                    username.setText ( user.getFirstName () );

                }

                @Override
                public void onCancelled (@NonNull DatabaseError databaseError) {

                }
            } );
        }


    }

消息 activityxml

<LinearLayout 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"
    tools:context=".MessageActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#4CAF50" />

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            />

    </com.google.android.material.appbar.AppBarLayout>

</LinearLayout>

调试器中显示的内容:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sportsbuddy, PID: 858
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.sportsbuddy.User.getFirstName()' on a null object reference
        at com.example.sportsbuddy.MessageActivity.onDataChange(MessageActivity.java:47)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
        at com.google.firebase.database.core.view.EventRaiser.run(com.google.firebase:firebase-database@@17.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6986)
        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:1445)

用户class:

public class User {
   public String mfirstName;
    public String msecondname;
    public String muid;
    public String memail;
    public User(){

    }



    public User (String firstName, String secondName, String uid, String email) {
        this.mfirstName = firstName;
        this.msecondname = secondName;
        this.muid=uid;
        this.memail=email;
    }

    public User (String firstName, String secondName) {
    }
@PropertyName ( "FIRST NAME" )
    public String getFirstName () {
        return mfirstName;
    }
    @PropertyName ("SECOND NAME" )
    public String getSecondName () {
        return msecondname;
    }
    @PropertyName ( "Uid" )
    public String getUid () {
        return muid;
    }
    @PropertyName ( "E-MAIL" )
    public String getEmail () {
        return memail;
    }
    @PropertyName ( "FIRST NAME" )
    public void setFirstName (String firstName) {
        this.mfirstName = firstName;
    }
    @PropertyName ( "SECOND NAME" )
    public void setSecondName (String secondName) {
        this.msecondname = secondName;
    }
    @PropertyName ( "Uid" )
    public void setUid (String uid) {
        this.muid = uid;
    }
    @PropertyName ( "E-MAIL" )
    public void setEmail (String email) {
        this.memail = email;
    }
}

我希望为每个用户显示一个空的 activity 而不是 app to crahs。

不是toolbar的问题,是数据库请求的问题。错误在这一行

User user=dataSnapshot.getValue (User.class);

用户为空。

您正在这样设置 reference

reference= FirebaseDatabase.getInstance ().getReference ("users").child ( "FIRST NAME");

所以这指向 /users/FIRST NAME,它在您的 JSON 中不存在。这意味着您的 dataSnapshot 将为空。

我的猜测是你想引用当前用户的节点,你这样做:

reference= FirebaseDatabase.getInstance ().getReference ("users").child (userid);

reference= FirebaseDatabase.getInstance ().getReference ("users").child (fuser.getUid());