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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在這種情況下如何刪除 NullpointerException?

在這種情況下如何刪除 NullpointerException?

富國(guó)滬深 2023-09-13 18:12:28
我正在使用 firebase 數(shù)據(jù)庫(kù),想要在列表視圖中顯示數(shù)據(jù)。當(dāng)我調(diào)用 firebase 數(shù)據(jù)庫(kù)的 onDataChange() 時(shí),它顯示空指針異常。我在谷歌和許多其他網(wǎng)站上找到了最好的方法,但沒(méi)有任何東西可以幫助我擺脫這個(gè)錯(cuò)誤,所以請(qǐng)幫助我,我在過(guò)去三個(gè)小時(shí)內(nèi)陷入了這個(gè)錯(cuò)誤......這是名為 Articles_from_firebase.java 的 Mainactiviy:public class Articles_from_fireabse extends AppCompatActivity {EditText editTextName;Spinner spinnerGenre;Button buttonAddArtist;ListView listViewArtists;DatabaseReference databaseArtists;//a list to store all the artist from firebase databaseList<Artist> artists;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_articles_from_fireabse);    databaseArtists = FirebaseDatabase.getInstance().getReference("artists");    editTextName = (EditText) findViewById(R.id.editTextName);    spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);    listViewArtists = (ListView) findViewById(R.id.listViewArtists);    buttonAddArtist = (Button) findViewById(R.id.buttonAddArtist);    buttonAddArtist.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            //calling the method addArtist()            //the method is defined below            //this method is actually performing the write operation            addArtist();        }    });}private void addArtist() {    //getting the values to save    String name = editTextName.getText().toString().trim();    String genre = spinnerGenre.getSelectedItem().toString();    //checking if the value is provided    if (!TextUtils.isEmpty(name)) {        //getting a unique id using push().getKey() method        //it will create a unique id and we will use it as the Primary Key for our Artist        String id = databaseArtists.push().getKey();        //creating an Artist Object       Artist artist = new Artist(id, name, genre);    }}
查看完整描述

2 回答

?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

簡(jiǎn)單的解決方案

每當(dāng)您調(diào)用artists.clear();檢查以確保它不為空時(shí)。

if(artists != null) 
  artists.clear();
  else
  artists = new ArrayList<>();

可持續(xù)解決方案

你應(yīng)該考慮對(duì)象 arry 的作用artists是什么。在本例中,您有一個(gè)數(shù)據(jù)庫(kù)和一個(gè)數(shù)組,它們都存儲(chǔ)有關(guān)藝術(shù)家的信息。數(shù)據(jù)庫(kù)應(yīng)該存儲(chǔ)持久數(shù)據(jù),即程序執(zhí)行后仍存在的數(shù)據(jù)。然而,您的artists對(duì)象在運(yùn)行時(shí)就存在。

因此,您應(yīng)該在程序開(kāi)始處編寫(xiě)代碼,將數(shù)據(jù)從數(shù)據(jù)庫(kù)加載到對(duì)象中artists。在運(yùn)行時(shí)引用/編輯/添加到artist對(duì)象。最后,在運(yùn)行時(shí)結(jié)束時(shí)使用清理代碼來(lái)更新數(shù)據(jù)庫(kù)中的藝術(shù)家表。


查看完整回答
反對(duì) 回復(fù) 2023-09-13
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

yes 初始化列表藝術(shù)家;在 onCreate(){} 內(nèi)部

像這樣的 Artists = new ArrayList<>();


查看完整回答
反對(duì) 回復(fù) 2023-09-13
  • 2 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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