Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlin.UninitializedPropertyAccessException: lateinit property tvTitle has not been initialized #172

Closed
thanhpd-teko opened this issue Feb 23, 2020 · 11 comments

Comments

@thanhpd-teko
Copy link

  • PlaceHolderView: 1.0.3
  • Kotlin: 1.3.61

`

@layout(R.layout.view_room_contract_item)
class RoomContractItemView(
val context: Context,
val contract: RoomContractsQuery.RoomContract,
val listener: ContractItemListener
) {

@View(R.id.tvContractItemTitle)
lateinit var tvTitle: TextView

@View(R.id.tvContractItemRoom)
lateinit var tvRoom: TextView

@View(R.id.tvContractItemAddress)
lateinit var tvAddress: TextView

@View(R.id.tvContractItemTime)
lateinit var tvTime: TextView

interface ContractItemListener {
    fun onContractClick(id: Int)
}

@Click(R.id.contract_item_container)
fun onClick() {
    listener.onContractClick(contract.id().toInt())
}

@Resolve
fun resolve() {
    tvTitle.text = context.getString(R.string.contract_title, contract.id().toInt())
    tvRoom.text = RoomUtils.getFullRoomName(
        context, contract.room()?.name(),
        contract.room()?.motel()?.name()
    )
    tvAddress.text = contract.room()?.motel()?.address() ?: EMPTY_STRING
    tvTime.text = context.getString(
        R.string.date_range,
        DateTimeUtils.formatDate(contract.startDate()),
        DateTimeUtils.formatDate(contract.endDate())
    )
}

}
Please help.

`

@thanhpd-teko
Copy link
Author

@janishar Please help, or guide me how to fix it!

@janishar
Copy link
Owner

janishar commented Feb 29, 2020

@thanhpd-teko Verify

  1. Check that @Layout has 'L' in Caps
  2. import of the @Layout and @View should be from placeholderview package.
  3. Also, check kapt is used for annotation processing
    apply plugin: 'kotlin-kapt'
    ...
    implementation 'com.mindorks.android:placeholderview:1.0.3'
    kapt 'com.mindorks.android:placeholderview-compiler:1.0.3'

@thanhpd-teko
Copy link
Author

@janishar Sorry for late reply. Below is full of my view file.
The problem is that it happened occasionally when developing, not all the time (after cleanup project it works for a while, then the error occured again...)

package com.frovis.vuitro.ui.main.issue.list

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.widget.TextView
import com.frovis.vuitro.MyAdminIssueQuery
import com.frovis.vuitro.R
import com.frovis.vuitro.utils.CommonUtils
import com.frovis.vuitro.utils.DateTimeUtils
import com.frovis.vuitro.utils.IssueUtils.styleIssueLevel
import com.frovis.vuitro.utils.RoomUtils.getFullRoomName
import com.mindorks.placeholderview.annotations.*

@Layout(R.layout.issue_item)
@NonReusable
class IssueItemView(
    private val context: Context?,
    private val issue: MyAdminIssueQuery.Result,
    private val listener: IssueItemListener
) {
    interface IssueItemListener {
        fun onIssueClick(id: String)

    }

    @View(R.id.tvIssueItemTitle)
    lateinit var tvIssueItemTitle: TextView
    @View(R.id.tvIssueItemRoom)
    lateinit var tvIssueItemRoom: TextView
    @View(R.id.tvIssueItemAddress)
    lateinit var tvIssueItemAddress: TextView
    @View(R.id.tvIssueItemCreator)
    lateinit var tvIssueItemCreator: TextView
    @View(R.id.tvIssueItemCreatedAt)
    lateinit var tvIssueItemCreatedAt: TextView
    @View(R.id.tvIssueItemLevel)
    lateinit var tvIssueItemLevel: TextView

    @Resolve
    fun onResolve() {
        tvIssueItemTitle.text = issue.title()
        tvIssueItemRoom.text = getFullRoomName(
            context,
            issue.room()?.name(),
            issue.room()?.motel()?.name()
        )
        tvIssueItemAddress.text = issue.room()?.motel()?.address() ?: ""
        tvIssueItemCreator.text = issue.creator()?.fullName() ?: ""
        tvIssueItemCreatedAt.text =
            DateTimeUtils.dateTimeUTCToLocalDate(issue.createdAt().toString())
        tvIssueItemLevel.text = CommonUtils.getStringResourceByKey(context, issue.level())
        val drawable = tvIssueItemLevel.background as GradientDrawable
        context?.let { styleIssueLevel(drawable, it, issue.level()) }
    }

    @Click(R.id.llIssueItemContainer)
    fun onContainerClick() {
        listener.onIssueClick(issue.id())
    }
}

Output error:
2020-03-03 06:47:23.975 18043-18043/com.frovis.vuitro.debug E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.frovis.vuitro.ui.main.issue.create.dialog.TextItemView$ViewBinder.bindClick(TextItemView$ViewBinder.java:48) at com.frovis.vuitro.ui.main.issue.create.dialog.TextItemView$ViewBinder.bindClick(TextItemView$ViewBinder.java:10)

@janishar
Copy link
Owner

janishar commented Mar 3, 2020

The error is in TextItemView can you share that class as well

@thanhpd-teko
Copy link
Author

@janishar Here you are.
TextItemView

package com.frovis.vuitro.ui.main.issue.create.dialog

import android.widget.TextView
import com.frovis.vuitro.R
import com.mindorks.placeholderview.annotations.*

@NonReusable
@Layout(R.layout.text_item)
class TextItemView(
    private val id: Int,
    private val text: String,
    private val listener: OnClickListener
) {

    @View(R.id.tv_item_text)
    lateinit var textView: TextView

    @Resolve
    fun onResolve() {
        textView.text = text
    }

    @Click(R.id.tv_item_text)
    fun onClick() {
        listener.onClick(id)
    }

    interface OnClickListener {
        fun onClick(itemId: Int?)
    }
}

text_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:background="@color/cardview_light_background">
	<TextView
			android:id="@+id/tv_item_text"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:padding="16dp"
			android:ellipsize="end"
			android:maxLines="1"
			android:textAppearance="?attr/textAppearanceSubtitle1"/>

</FrameLayout>

Fragment

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel.motelData.observe(viewLifecycleOwner, Observer { motels ->
            if (motels != null) {
                phvMotelList.removeAllViews()
                for (motel in motels) {
                    phvMotelList.addView(motel.id?.let {
                        TextItemView(
                            it,
                            motel.name,
                            this
                        )
                    })
                }
            }
        })

@janishar
Copy link
Owner

janishar commented Mar 4, 2020

Everything's appears to be correct. I am not able to find the issue with these references.

@thanhpd-teko
Copy link
Author

Thank you. I will investigate, find out more evidence about this issue. I will let you know if i find anything intesting.

@thanhpd-teko
Copy link
Author

@janishar
More information about error: E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Below is generated class:

 @Override
  protected void bindClick(final RoomContractItemView resolver, View itemView) {
    itemView.findViewById(R.id.onClick_1).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        resolver.onClick();
      }
    });
  }

@janishar
Copy link
Owner

janishar commented Mar 6, 2020

@thanhpd-teko If you could send me the zip of your project to my email janishar.ali@gmail.com then I will be able to run it and debug.

@thanhpd-teko
Copy link
Author

Hi @janishar , please check email. Thanks.

@hayahyts
Copy link

@janishar The same thing happened to me. I just had to Clean Project and Build again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants