Mirmukhsin Sodikov
04/01/2020, 7:05 PMVariable mOnClickListener must be initialized
and underlines the mOnClickListener
inside the init
statement.
I have been doing the same thing in Java and it didn’t say anything like that.
Am I missing something here ?
EDIT : I am trying to make a custom layout here. I have been doing this in Java all the time - assigned OnClickListener
to a private variable and then setting it to a button in the constructor.
class CustomLayout(context: Context) : FrameLayout(context) {
companion object {
const val RESOURCE = R.layout.layout_custom
}
init {
LayoutInflater.from(context).inflate(RESOURCE, this)
// IntelliJ underlined `mOnClickListener` here and it says : "Variable mOnClickListener must be initialized"
btnSend.setOnClickListener(mOnClickListener)
}
private val mOnClickListener = OnClickListener { context.toastInfo("Info") }
}
Java version of the code which works fine
public class CustomLayout extends FrameLayout{
public CustomLayout(){
// inflating the xml layout
...
btnSend.setOnClickListener(mOnClickListener);
}
private OnClickListener mOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
};
}
Adam Powell
04/01/2020, 7:18 PMm
🙂Mirmukhsin Sodikov
04/01/2020, 7:24 PMm
is gonna be the hardest one for me 😄Adam Powell
04/01/2020, 7:27 PMm
in a getter/setter, so you don't need them hereMirmukhsin Sodikov
04/01/2020, 7:35 PM