第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果用戶尚未登錄,如何為 getDisplayName() 寫入 if 語句?

如果用戶尚未登錄,如何為 getDisplayName() 寫入 if 語句?

紅顏莎娜 2023-09-06 17:03:29
我在運(yùn)行我的應(yīng)用程序時遇到了這個問題:嘗試在空對象引用上調(diào)用虛擬方法 java.lang.String com.google.firebase.auth.FirebaseUser.getDisplayName()我想制作一個用戶無需先登錄即可瀏覽的應(yīng)用程序。如何將name.setText("hi, " + currentUser.getDisplayName());if 語句放在里面 我應(yīng)該在 if 語句的編碼中寫什么?這是我的MainActivityjava類:@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    name=findViewById(R.id.name);    //        //display customer's account name    name.setText("hi, " + currentUser.getDisplayName());    //ini    firebaseAuth = FirebaseAuth.getInstance();    currentUser = firebaseAuth.getCurrentUser();    databaseReference = FirebaseDatabase.getInstance().getReference("Cust");@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.menu_option,menu);    return true;}@Overridepublic boolean onOptionsItemSelected(@NonNull MenuItem item) {    int id= item.getItemId();    if (id == R.id.logout){        logout();        return true;    }    else if (id==R.id.login){        login();        return true;    }    return super.onOptionsItemSelected(item);}private void login() {    Intent intent= new Intent(this,LoginActivity.class);    startActivity(intent);}private void logout() {    FirebaseAuth.getInstance().signOut();    Intent intent = new Intent(MainActivity.this, LoginActivity.class);    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);    startActivity(intent);    finish();}}
查看完整描述

1 回答

?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個贊

檢查 currentUser 是否不為空:


if (currentUser != null) name.setText("hi, " + currentUser.getDisplayName());

而且,在您的代碼中,您在從 firebase 初始化它getDisplayName 之前要求它,因此它始終為空:


firebaseAuth = FirebaseAuth.getInstance();

currentUser = firebaseAuth.getCurrentUser();

databaseReference = FirebaseDatabase.getInstance().getReference("Cust");


if (currentUser != null) name.setText("hi, " + currentUser.getDisplayName());


查看完整回答
反對 回復(fù) 2023-09-06
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號