Button is always null when trying to find it? - kotlin

I have this code:
val btn_click_me = findViewById(R.id.button1) as? Button
btn_click_me?.setOnClickListener {
// do something
}
which gives me this error:
java.lang.NullPointerException: null cannot be cast to non-null type android.widget.Button even though it exists in my layout file
the button is always null. how can i fix ? how can I set on click listener to button?
doing this from fragment...

You need context to find your ViewById. I recommend doing with requireContext() like this
val btn_click_me = requireContext().findViewById(R.id.button1) as? Button
btn_click_me?.setOnClickListener {
// do something
}
Also, if you wanna look into other ways of doing this, I recommend looking for viewBinding

Related

How to retrigger focusRequester.requestFocus() when coming back to a composable?

I have a MyBasicTextField in a composable to request user input:
#Composable
fun MyBasicTextField() {
val keyboardController = LocalSoftwareKeyboardController.current
val focusRequester = remember{ FocusRequester() }
BasicTextField(
modifier = Modifier
.focusRequester(focusRequester),
keyboardActions = keyboardActions ?: KeyboardActions(onAny = { keyboardController?.hide() }),
)
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}
The keyboard automatically slides in when showing this composable, always.
But wherever MyBasicTextField is used:
I tap on a LinkifiedText to leave and open a browser to show link
I tap BACK
and come back to previous MyBasicTextField screen, the keyboard is not shown
also the focusRequester.requestFocus() is not triggered again when coming back
How can I solve my issue?
Create a top-level variable in your activity, then modify it from within the onStart overridden method. Use that variable as the key for LaunchedEffect in place of Unit. That variable basically keeps track of when the user enters the app.
var userIn by mutableStateOf (true)
In your Composable,
Launched effect(userIn){
if(userIn && isKeyboardShown){
...
}
}
Boring,
You can use my answer here as well, but instead of incrmenting the launchKey every time it's called, only increment the launchKey once user clicks on the browser link, that way it will not pop up during other re-compositions.

How to open (enter) SettingsFragment or any Fragment on Android (Kotlin)

I am learning Android programing, and I can not figure it out how to open settings fragment when button is clicked.
This fragment is not in the navigation map, so it is not possible to connect them in navigation and to use findNavController().navigate(actionFromTo)
As explained on developer guide: https://developer.android.com/guide/topics/ui/settings
I have created fragment:
class PreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
What I need to write in click listener to enter settings fragment?
Click listener is:
binding.buttonSettings.setOnClickListener {
}
I have tried with to use code in developers guide:
binding.buttonSettings.setOnClickListener {
val fragmentManager: FragmentManager? = activity?.supportFragmentManager
fragmentManager?.beginTransaction()?.replace(R.id.preferencesFragment, PreferencesFragment())?.commit()
}
But program crashes when button is pressed, with error:
No view found for...
The issue seems to be the content ID you want to replace, therefore you need to pass the current content ID you want to replace
see below code, use android.R.id.content instead of R.id.preferencesFragment
binding.buttonSettings.setOnClickListener {
val fragmentManager: FragmentManager? = activity?.supportFragmentManager
fragmentManager?.beginTransaction()?.replace(android.R.id.content, PreferencesFragment())?.commit()
}

JavaFX TextField causes NullPointerException

I am using a Button with an event to control the input of a user, but I am having trouble checking whether or not the TextField is empty.
The TextField and Button are declared before the button event, like
TextField svar = new TextField();
Button submitB = new Button("submit");
This is my code:
submitB.setOnAction((event) -> {
if (svar.getText().equals(null) {
primaryStage.setTitle("No input!");
}
if (svar.getText().subSequence(0, 1).toString().toLowerCase().equals(animals.get(pic).getName().subSequence(0, 1).toString().toLowerCase())) {
primaryStage.setTitle("RÄTT!");
bild.setImage(new Image(animals.get(pic+=1).getImgsrc()));
svar.setText(null);
svar.requestFocus();
}
else
{
primaryStage.setTitle("FEL!");
svar.setText(null);
svar.requestFocus();
}
});
This piece of the code above is what in my mind should handle a situation where the TextField is empty:
if (svar.getText().equals(null) {
primaryStage.setTitle("No input!");
}
However I am still getting a NullPointerException no matter what. I've tried some different solutions which have all failed and I'm hoping someone can point me in the right direction. Thanks!
Instead of calling equals to check for null, you should use "==" like:
if (svar.getText() == null) {
Reason you get NPE (NullPointerException) is, since as you say svar.getText() is null, you are trying to call equals on null and a reason for NPE.
The correct way to do it, is not check for Null, but to check for a empty String:
if (svar.getText().equals("") {
primaryStage.setTitle("No input!");
}
Some prefer:
if ("".equals(svar.getText() {
primaryStage.setTitle("No input!");
}

toggle (hide/show) in Extjs4

I am trying to do something like : when user click on the button, the child panel will show/hide
the issue is the 'onbtnClick' function is working just once. when i click on the button the panel shows and then when i click it again nothing happens and no errors tho ! the panel should hide
By the looks of it, there isn't really much need to pass a boolean param to the function.
If you purely want a 'toggle' function, based on the panels visibility and you have a reference to the Ext component, you can use the isVisible() function:
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.panel.Panel-method-isVisible
So your onBtnClick function would look something like this:
onbtnClick: function (){
var childPanel = Ext.getCmp('p');
if(childPanel.isVisible()) {
childPanel.hide();
}
else {
childPanel.show();
}
}
onbtnClick: function (){
var childPanel = Ext.getCmp('p');
if (!childPanel.isHidden()) {
childPanel.hide();
} else {
childPanel.show();
}
}
Instead isVisible() use method isHidden() because method isVisible may return false when the panel is covered by other components or is not rendered yet (even when your panel has not got hidden property (hidden = false)).
panel.getEl().toggle();
will serve your purpose.
-YP
you have setVisible, taking a boolean parameter to know what to do.
childPanel.setVisible( ! childPanel.isVisible() )
This example will actually toggle the visibility at each execution.

ExtJS 4 GroupingSummary How To Deactivate Expand on Summary Row Click

I have an ExtJS 4 grid with GroupingSummary Feature. ExtJs Default is grid is expanded when user clicks on any cell in grouping row. I want to disable this feature, no expansion should take place except when user clicks on designated 'expand' icon. How do I accomplish this?
There is the 'groupclick' event, is there a way to override its default behaviour and do nothing / remove it?
Thanks in advance
The method for it appears to be inherited from Grouping rather than GroupingSummary, so I suspect you need to override this method to prevent the current behaviour:
onGroupClick: function(view, group, idx, foo, e) {
var me = this,
toggleCls = me.toggleCls,
groupBd = Ext.fly(group.nextSibling, '_grouping');
if (groupBd.hasCls(me.collapsedCls)) {
me.expand(groupBd);
} else {
me.collapse(groupBd);
}
So you will need another file with something similar to the following:
Ext.require('Ext.grid.Grouping');
Ext.define('your.class.here', {
override: 'Ext.grid.Grouping',
onGroupClick: function() {
//do nothing
});
)};
Then you should be able to write a function that mimicks it was doing on groupclick, but instead on your icon click. Hope that helps.