亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何修復空片段?

如何修復空片段?

慕的地8271018 2024-01-17 20:54:17
我是 Android 開發新手,正在嘗試創建自己的應用程序。它應該使用 YouTube 數據 API 顯示特定的 YouTube 頻道。我從 Android Studio 中的標準底部導航模板開始,并使用 Github 上的以下項目來獲取一些啟動幫助。https://github.com/stressGC/Remake-YouTube-Android我必須更改一些內容,例如代碼中已棄用的 http 調用,以使其與新的 Android APK 一起運行。從我的角度來看,一切似乎都很好:我可以看到 API 內容看起來不錯,并且每個標題/描述/發布日期都放置在相應的變量中。日志中也沒有錯誤消息。當我啟動模擬器時,應用程序運行良好。但是當我切換到“儀表板”片段(放置代碼的地方)時,它是空的。
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

你的內部RequestYouTubeAPI ASyncTask有這個錯誤代碼:


        } catch (IOException e) {

            e.printStackTrace();

            return null;

        }

然后onPostExecute你有以下內容:


    @Override

    protected void onPostExecute(String response) {

        super.onPostExecute(response);

        if(response != null){

            try {

                JSONObject jsonObject = new JSONObject(response);

                Log.e("response", jsonObject.toString());

                mListData = parseVideoListFromResponse(jsonObject);

                initList(mListData);

                //adapter.notifyDataSetChanged();

            } catch (JSONException e) {

                e.printStackTrace();

            }

        }

    }

因此,如果您收到錯誤,return null并且onPostExecute收到響應, null則不會執行任何操作。


所以這個地方可能會出現錯誤,因此會出現空白片段。


在修復此問題之前,您可以證明這種情況正在發生,如下所示:


    @Override

    protected void onPostExecute(String response) {

        super.onPostExecute(response);

        if(response == null){

            Log.e("TUT", "We did not get a response, not updating the UI.");

        } else {

            try {

                JSONObject jsonObject = new JSONObject(response);

                Log.e("response", jsonObject.toString());

                mListData = parseVideoListFromResponse(jsonObject);

                initList(mListData);

                //adapter.notifyDataSetChanged();

            } catch (JSONException e) {

                e.printStackTrace();

            }

        }

    }

您可以通過兩種方式解決此問題:


將doInBackground捕獲更改為:


        } catch (IOException e) {

            Log.e("TUT", "error", e);

            // Change this JSON to match what the parse expects, so you can show an error on the UI

            return "{\"yourJson\":\"error!\"}";

        }

或者onPostExecute:


        if(response == null){

            List errorList = new ArrayList();

            // Change this data model to show an error case to the UI

            errorList.add(new YouTubeDataModel("Error");

            mListData = errorList;

            initList(mListData);

        } else {

            try {

                JSONObject jsonObject = new JSONObject(response);

                Log.e("response", jsonObject.toString());

                mListData = parseVideoListFromResponse(jsonObject);

                initList(mListData);

                //adapter.notifyDataSetChanged();

            } catch (JSONException e) {

                e.printStackTrace();

            }

        }

希望有所幫助,代碼中可能還有其他錯誤,但如果 API、Json、授權、互聯網等存在問題,則可能會發生這種情況。


查看完整回答
反對 回復 2024-01-17
  • 1 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號