gpt4 book ai didi

java - 从 firebase 获取名称时 model_user 中获取 null - AndroidX

转载 作者:行者123 更新时间:2023-12-02 09:25:08 24 4
gpt4 key购买 nike

当我在 MainActivity 中时,当使用 model_user 从 firebase 设置用户名时,我得到 null,这是我使用的代码

//MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

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

<androidx.appcompat.widget.Toolbar
android:id="@+id/Toolbar_Main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/profile_image"
android:src="@drawable/profile_blank"/>



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"
android:textSize="18sp"
android:layout_marginLeft="25dp"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginStart="25dp" />

</androidx.appcompat.widget.Toolbar>

<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
app:tabSelectedTextColor="#fff"
app:tabIndicatorColor="#fff"
app:tabTextColor="#fff"/>

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

<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</LinearLayout>

//MainActivity.java

public class MainActivity extends AppCompatActivity {


CircleImageView profile_image;
TextView username;

FirebaseUser firebaseUser;
DatabaseReference reference;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar_main = findViewById(R.id.Toolbar_Main);
setSupportActionBar(toolbar_main);
getSupportActionBar().setTitle("Login");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);


profile_image = findViewById(R.id.profile_image);
username = findViewById(R.id.username);

firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());

reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ModelUser modelUser = dataSnapshot.getValue(ModelUser.class);
username.setText(modelUser.getUsername());
if (modelUser.getImageURL().equals("default")){
profile_image.setImageResource(R.mipmap.ic_launcher);
} else {

//change this
Glide.with(getApplicationContext()).load(modelUser.getImageURL()).into(profile_image);
}
}

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

}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar, menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_logout:
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, StartActivity.class));
finish();
return true;
}
return false;
}

}

这个模型似乎变得无效

模型用户.java

public class ModelUser {
private String id;
private String username;
private String imageURL;

public ModelUser(String id, String username, String imageURL) {
this.id = id;
this.username = username;
this.imageURL = imageURL;
}


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getImageURL() {
return imageURL;
}

public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
}

似乎 Model_user 变得为空,但我不知道为什么会导致它

最佳答案

删除 getter-setter 并添加空构造函数

模型用户.java

  public class ModelUser {
public String id;
public String username;
public String imageURL;

public ModelUser(String id, String username, String imageURL) {
this.id = id;
this.username = username;
this.imageURL = imageURL;
}

public ModelUser() {
}
}

试试这个

关于java - 从 firebase 获取名称时 model_user 中获取 null - AndroidX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58393842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com