LoadingButtonAndroid icon indicating copy to clipboard operation
LoadingButtonAndroid copied to clipboard

lateinit property initialState has not been initialized

Open flo5324 opened this issue 6 years ago • 8 comments

Hi,

I encountered a problem with CircularProgressButton I wanted to try the behavior of my button, so I called startAnimation() in the onCreate() but I get this exception : lateinit property initialState has not been initialized If I call it after (like in an onClickListener) it works fine

As sample I used a simple project with an empty activity :

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.startAnimation()
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">

	<br.com.simplepass.loadingbutton.customViews.CircularProgressButton
		android:id="@+id/button"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:text="Hello World!"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintLeft_toLeftOf="parent"
		app:layout_constraintRight_toRightOf="parent"
		app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

kotlin 1.3 AndroidX LoadingButtonAndroid 2.0.7

Best regards

flo5324 avatar Feb 14 '19 14:02 flo5324

having the same issue when calling dispose()

balamuthu1 avatar Feb 25 '19 14:02 balamuthu1

So, for those who have the same issue I found a workaround :

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // This will manually initialize the 'initialState' field
        button.saveInitialState()

        button.startAnimation()
    }
}

flo5324 avatar Feb 26 '19 13:02 flo5324

Yes, I am getting the same issue. I have a CircularProgressButton inside a DialogFragment.

It appears that it is only observing the lifecycle of the underlying activity, therefore, it crashes as soon as the activity ends, as the button has been disposed of but the lifecycle listener is still alive.

Any ideas for a workaround?

paulsUsername avatar Apr 17 '19 12:04 paulsUsername

I have resolved this issue by adding the bottom programmatically and passing in the context of the Dialog Fragment instead of the activity context it seems to be receiving via XML.

Adding it like so:

    val height = (35).scaleToDP(safeContext)
    val width = (150).scaleToDP(safeContext)
    val layoutParams = LinearLayout.LayoutParams(width, height)
    val button = CircularProgressButton(safeContext)
    button.layoutParams = layoutParams
    button.elevation = (7).scaleToDP(safeContext).toFloat()
    val drawable = ContextCompat.getDrawable(safeContext, R.drawable.download_button_shape)
    button.background = drawable
    button.text = getString(R.string.offline_map_download_text)
    button.setOnClickListener {
        processDownloadDeleteButtonTapped(button)
    }
    return button

I have no issues now.

paulsUsername avatar Apr 17 '19 13:04 paulsUsername

It took me 3 months to see this issue o.O. I'll be taking a look

leandroBorgesFerreira avatar May 16 '19 20:05 leandroBorgesFerreira

Use the version 2.1.3, this is fixed in that one =]

leandroBorgesFerreira avatar May 16 '19 21:05 leandroBorgesFerreira

I have the same issue with version 2.2.0, did the bug come back?

Kalianey avatar Apr 15 '20 15:04 Kalianey

I have the same issue with version 2.2.0, did the bug come back?

Having the same issue. Please use button.saveInitialState()

basusingh avatar Aug 09 '20 18:08 basusingh