Android: импорт kotlin синтетического сбоя, даже если идентификатор присутствует в макете - PullRequest
0 голосов
/ 16 ноября 2018

Я новичок в Kotlin и в своем проекте я использую функцию синтетического импорта kotlins. Это сбой, иногда выбрасывая KotlinNullPointerException. Вот журнал:

Fatal Exception: kotlin.KotlinNullPointerException
   at main.fragments.ListingDetailsFragment$getProductComboData$responseListener$1.onResponse(ListingDetailsFragment.kt:1607)
   at main.fragments.ListingDetailsFragment$getProductComboData$responseListener$1.onResponse(ListingDetailsFragment.kt:159)
   at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:83)
   at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
   at android.os.Handler.handleCallback(Handler.java:790)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:187)
   at android.app.ActivityThread.main(ActivityThread.java:7025)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:514)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)

Код в строке №. 1607

lin_product_combo!!.visibility = View.GONE

Это происходит после вызова веб-службы. Ниже приведена функция:

private fun getProductComboData() {
    val params = HashMap<String, String>()
    params["listing_id"] = listingId
    params["category_id"] = listingDetailsModel!!.category_id
    params["condition"] = listingDetailsModel!!.condition
    params["location"] = listingDetailsModel!!.location

    val responseListener = Response.Listener<JSONObject> { response ->
                    try {
            val code = response.getString("code")
            if (code.equals("success", ignoreCase = true)) {
                val data = response.optJSONObject("data")
                if (data != null) {
                    productComboModels!!.clear()
                    if (data.has("currentLid") && data.get("currentLid") is JSONObject) {
                        val currentLidObj = data.getJSONObject("currentLid")
                        val recommendateDataArray = data.getJSONArray("recommendateData")
                        if (recommendateDataArray != null) {
                            val size = recommendateDataArray.length()
                            if (size > 0) {
                                val currentLidModel = BuyListingsModel.getBuyListingModel(currentLidObj)
                                currentLidModel.isItemSelected = true
                                productComboModels!!.add(currentLidModel)
                                for (i in 0 until size) {
                                    val recommendedDataModel = BuyListingsModel
                                            .getBuyListingModel(recommendateDataArray.optJSONObject(i))
                                    recommendedDataModel.isItemSelected = true
                                    productComboModels!!.add(recommendedDataModel)
                                }
                            }
                        }
                    }
                    if (!productComboModels!!.isEmpty()) {
                        frequently_bought_together_title!!.visibility = View.VISIBLE
                        lin_product_combo!!.visibility = View.VISIBLE
                        productComboAdapter!!.notifyDataSetChanged()
                        total_combo_price!!.text = Util
                                .formatCurrencyToRupees(productComboPrice.toString())
                    } else {
                        lin_product_combo!!.visibility = View.GONE
                    }
                }
            } else if (code.equals("failed", ignoreCase = true)) {
                handleError(response)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    val errorListener = Response.ErrorListener { error -> error.printStackTrace() }
    comboAPICalled = true
    Api.getFrequentlyBoughtTogetherData(params, responseListener, errorListener)
}

Это операторы импорта:

import kotlinx.android.synthetic.main.certification_package_eco_layout.*
import kotlinx.android.synthetic.main.certification_package_history_layout.*
import kotlinx.android.synthetic.main.certification_packages_locked_section.*
import kotlinx.android.synthetic.main.fragment_listing_details_v2.*
import kotlinx.android.synthetic.main.fragment_listing_details_v2.view.*
import kotlinx.android.synthetic.main.layout_discovery_tools_view.*
import kotlinx.android.synthetic.main.layout_fcts_panel.* 
import kotlinx.android.synthetic.main.layout_tco_value_view.*
import kotlinx.android.synthetic.main.ldp_listing_summary_layout.*
import kotlinx.android.synthetic.main.pay_token_amount_layout.*
import kotlinx.android.synthetic.main.product_detail_seller_panel.*
import kotlinx.android.synthetic.main.service_date_time_panel.*

И lin_product_combo - это линейный макет, который присутствует в XML-файле. Не получаю идеи для исключения нулевого указателя. То же самое произошло и с некоторыми другими взглядами.

Ответы [ 2 ]

0 голосов
/ 16 ноября 2018

На практике хорошо, что вы должны выполнять это кодирование в onActivityCreated (), а не в onCreateView ().

в основном onActivityCreated () вызывается после onCreate () в Activity,

поэтому все переменные и событие onClick должны быть определены в onActivityCreated (), а не в onCreateView ().

Спасибо, Зафар Хуссейн

0 голосов
/ 16 ноября 2018

Если вы используете фрагмент, вы должны использовать Create View

class Profil_Fragment : Fragment(){


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.profil_fragment, container, false)

        // Add view
        view.lin_product_combo!!.visibility = View.GONE



        return view
    }


    private fun getProductComboData(){
        ...

        view.lin_product_combo!!.visibility = View.GONE

        ...
    }


}
...