leandroBorgesFerreira / LoadingButtonAndroid

A button to substitute the ProgressDialog
MIT License
1.94k stars 214 forks source link

lateinit property initialState has not been initialized #119

Open flo5324 opened 5 years ago

flo5324 commented 5 years ago

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

balamuthu1 commented 5 years ago

having the same issue when calling dispose()

flo5324 commented 5 years ago

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()
    }
}
paulsUsername commented 5 years ago

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 commented 5 years ago

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.

leandroBorgesFerreira commented 5 years ago

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

leandroBorgesFerreira commented 5 years ago

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

Kalianey commented 4 years ago

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

basusingh commented 4 years ago

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

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