【问题标题】:Populate Listview from Google Books JSON从 Google 图书 JSON 填充 Listview
【发布时间】:2018-06-13 13:25:12
【问题描述】:

我正在使用 Google Books API 来搜索图书。但问题是,当我希望在我的 ListView 中填充书籍时,我会收到 错误onPostExecute 指出了这个错误,但我不知道是什么问题。

 kotlin.KotlinNullPointerException
    at com.example.claudiu.reader.Fragments.ISBNFragment$FetchBookTask.onPostExecute(ISBNFragment.kt:137)
    at com.example.claudiu.reader.Fragments.ISBNFragment$FetchBookTask.onPostExecute(ISBNFragment.kt:56)

这是我设置适配器的地方:

 override fun onPostExecute(books: List<Book>?) {
        if (books != null) {
            adapter!!.clear()
            for (book in books) {
                adapter!!.add(book)
            }
        }
    }

这是我解析 JSON 的所有代码:

  @Throws(JSONException::class)
    private fun getBookDataFromJson(booksJsonStr: String?): List<Book> {

        val books = ArrayList<Book>()

        val API_RESULT_ITEMS_ARRAY = "items"
        val API_VOLUME_INFO = "volumeInfo"
        val API_BOOK_TITLE = "title"
        val API_BOOK_IMAGE_LINKS = "imageLinks"
        val API_BOOK_SMALL_THUMBNAIL = "smallThumbnail"
        val API_BOOK_AUTHORS_ARRAY = "authors"

        val booksJson = JSONObject(booksJsonStr)
        val itemsArray = booksJson.getJSONArray(API_RESULT_ITEMS_ARRAY)

        for (i in 0 until itemsArray.length()) {

            val item = itemsArray.getJSONObject(i)
            val volumeInfo = item.getJSONObject(API_VOLUME_INFO)

            val bookTitle = volumeInfo.getString(API_BOOK_TITLE)

            val imageLinksSB = StringBuilder()
            if (!volumeInfo.isNull(API_BOOK_IMAGE_LINKS)) {
                val imageLinks = volumeInfo.getJSONObject(API_BOOK_IMAGE_LINKS)
                imageLinksSB.append(imageLinks.getString(API_BOOK_SMALL_THUMBNAIL))
            } else {
                imageLinksSB.append("-1")
            }
            val bookImageLink = imageLinksSB.toString()

            val authorsSB = StringBuilder()
            if (!volumeInfo.isNull(API_BOOK_AUTHORS_ARRAY)) {
                val authorsArray = volumeInfo.getJSONArray(API_BOOK_AUTHORS_ARRAY)
                for (k in 0 until authorsArray.length()) {
                    authorsSB.append(authorsArray.getString(k))
                    authorsSB.append(getString(R.string.comma))
                }
            } else {
                authorsSB.append(getString(R.string.unknown_error))
            }
            val bookAuthors = authorsSB.toString()

            books.add(Book(bookTitle, bookAuthors, bookImageLink))
        }

        Log.d(LOG_TAG, "BOOKS : $books")
        return books
    }

我找不到任何可以帮助我的东西,我也不知道该怎么办。

【问题讨论】:

  • adapternull
  • 我该怎么办?能给个建议吗?

标签: android json listview kotlin google-books


【解决方案1】:

你的问题是你声明适配器的地方,我遇到这个问题是因为我的适配器声明在错误的地方。

您应该将适配器声明从 onCreatView 移到 onViewCreated 中,一切都会正常工作。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多