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

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

為什么我會(huì)在這段代碼中得到 nullPointerException?

為什么我會(huì)在這段代碼中得到 nullPointerException?

回首憶惘然 2022-06-15 14:39:01
我有一個(gè)活動(dòng),并且在單擊方法的最后一行得到一個(gè) nullPointerExceptionpublic class MainActivity extends AppCompatActivity {List<Item> it;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);       }    public void clicked(View view) {                  getCompany(s);        Integer.toString(it.size());      }    public void getCompany(String name) {        apiCall.setCallback(new ApiCall.NetworkCallback() {            @Override            public void onResponse(List<Item> items) {                 it = items;                for (int i = 0; i < items.size(); i++) {                        Log.d("Name", it.get(i).getTitle());                }            }        });    }我刪除了一些代碼以保持簡(jiǎn)短,但問(wèn)題是在 onResponse 方法中可以訪問(wèn)“it”列表,但在 clicked 方法中給出了 nullPointerException。什么會(huì)導(dǎo)致這種情況以及如何存儲(chǔ)要在 onResponse 方法之外使用的列表?
查看完整描述

3 回答

?
慕蓋茨4494581

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

因?yàn)槟鷩L試在初始化之前獲取列表的大小。在 Integer.toString(it.size());

因?yàn)樗€沒(méi)有初始化 getCompany(s); 是異步方法嘗試刪除 Integer.toString(it.size());

因?yàn)樵诓皇褂煤蜎](méi)有用處。公共類 MainActivity 擴(kuò)展 AppCompatActivity {


List<Item> it;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);   

}


public void clicked(View view) {          

    getCompany(s);

}


public void getCompany(String name) {


    apiCall.setCallback(new ApiCall.NetworkCallback() {

        @Override

        public void onResponse(List<Item> items) {

             it = items;

            for (int i = 0; i < items.size(); i++) {    

                Log.d("Name", it.get(i).getTitle());

            }

        }

    });

}


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
PIPIONE

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

我會(huì)直接為您提供一個(gè)可能的解決方案,它為您提供一個(gè)界面的快速示例


public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);   

    }


    public void clicked(View view) {          

        getCompany(s, new OnReceiveCompany() {

            @Override

            public void onReceiveCompany(List<Item> it) {

                Integer.toString(it.size());                  

            }

        });

    }


    public void getCompany(String name, final OnReceiveCompany callback) {

        apiCall.setCallback(new ApiCall.NetworkCallback() {

            @Override

            public void onResponse(List<Item> items) {

                for (int i = 0; i < items.size(); i++) {    

                    Log.d("Name", it.get(i).getTitle());

                }

                // make sure this run on UI thread if you need to perform ui operation. You can wrap it with new Hanler(Looper.getMainLooper).post(...

                callback.onReceiveCompany(items);

            }

        });

    }


    interface OnReceiveCompany {

        void onReceiveCompany(List<Item> it);

    } 

}

為什么您的代碼不起作用:


    public void clicked(View view) {

        // this method is gonna set "it" in the block of code in onResponse, which will happens after you call Integer.toString probably in a separate thread

        getCompany(s);

        Integer.toString(it.size());  

    }


查看完整回答
反對(duì) 回復(fù) 2022-06-15
?
交互式愛(ài)情

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

當(dāng)您嘗試使用空變量(或尚未初始化的變量)時(shí),會(huì)發(fā)生 NullPointerException。

即使onResponse()從內(nèi)部調(diào)用clicked(),您也必須記住這onResponse是在異步調(diào)用中。這僅僅意味著不能保證在調(diào)用onResponse之前完成執(zhí)行。clicked()

我建議添加Integer.toString(it.size())到您的onResponse()方法中。

我希望這有幫助。不要再掙扎了 ;-) 快樂(lè)的編碼!


查看完整回答
反對(duì) 回復(fù) 2022-06-15
  • 3 回答
  • 0 關(guān)注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報(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)