簡體   English   中英

Android:即使布局中存在id,kotlin合成導入也會崩潰

[英]Android: kotlin synthetic import crashes even if id is present in layout

我是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

這在webservice調用之后發生。 下面是功能:

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文件中存在的線性布局。 不了解空指針異常。 對於某些球視圖也是如此。

在實踐中,最好在onActivityCreated()中而不是onCreateView()中進行此編碼。

基本上,onActivityCreated()是在Activity中的onCreate()之后調用的,

這就是為什么所有變量和onClick事件都必須在onActivityCreated()而不是onCreateView()中定義的原因。

謝謝,Zafar Hussain

如果使用片段,則必須使用創建視圖

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

        ...
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM