Set button listener in dialog

Cobold

I'm creating a dialog like this:

    private void MyDialogFragment() {

            LayoutInflater layoutInflater = LayoutInflater.from(this);
            View promptView = layoutInflater.inflate(R.layout.mydialog, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setView(promptView);        

            final RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
            ...

And after that I want to set the OnCheckedChangeListener:

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                ...
            }
        });

But whenever I try to show the dialog, it crashes my app. The log says:

Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference

So I'm guessing the radiogroup isn't created yet when I'm trying to set the listener. But how do I set it then?

natario

Try replacing

RadioGroup rg = (RadioGroup) findViewById(R.id.rg);

with

RadioGroup rg = (RadioGroup) promptView.findViewById(R.id.rg);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Dialog doesn't trigger the dismiss listener on a button press

分類Dev

How to set custom button in custom dialog box?

分類Dev

Give Id to every looped button and set on click listener based on button's id

分類Dev

#Button new on click listener

分類Dev

Listview button click listener is not working

分類Dev

Android Button Onclick listener not working

分類Dev

LIBGDX Android - button listener not working

分類Dev

How to interrupt a thread by button listener

分類Dev

Swift - How can I add an event listener for cancelling an email dialog?

分類Dev

Exclude Button From Event Listener OnClick

分類Dev

How do I add a button listener in Java?

分類Dev

Error when setting a click listener to a button

分類Dev

How to set up a listener for a variable in Kotlin

分類Dev

Pop up Dialog box on Pressing button in flutter

分類Dev

Reactjs: Dialog open is not working on button click

分類Dev

dialog buttons with count-down for default button

分類Dev

Android: Change a Button's text in a Dialog

分類Dev

Function not responding to button click in a Google HTML Dialog

分類Dev

jQuery: how to set selected in jQuery dialog?

分類Dev

yacal.js next/back button removes jquery event listener

分類Dev

How to set databaseInstanceName in Database Trace Listener from C# code?

分類Dev

How do you set a Menu item listener (onMenuItemSelected) in a FragmentPagerAdapter?

分類Dev

Android Custom View: How to set click listener on individual widgets

分類Dev

Closing alert dialog in android on press of button for first time

分類Dev

How do i bind action to a dialog "OK" button in Codename

分類Dev

angular 8 material dialog close button with X top right

分類Dev

jQuery UI Dialog Content being shown in button section

分類Dev

NullPointerException when dialog positive button onClick calls interface

分類Dev

How to show Application Launcher Dialog when do tap on button

Related 関連記事

  1. 1

    Dialog doesn't trigger the dismiss listener on a button press

  2. 2

    How to set custom button in custom dialog box?

  3. 3

    Give Id to every looped button and set on click listener based on button's id

  4. 4

    #Button new on click listener

  5. 5

    Listview button click listener is not working

  6. 6

    Android Button Onclick listener not working

  7. 7

    LIBGDX Android - button listener not working

  8. 8

    How to interrupt a thread by button listener

  9. 9

    Swift - How can I add an event listener for cancelling an email dialog?

  10. 10

    Exclude Button From Event Listener OnClick

  11. 11

    How do I add a button listener in Java?

  12. 12

    Error when setting a click listener to a button

  13. 13

    How to set up a listener for a variable in Kotlin

  14. 14

    Pop up Dialog box on Pressing button in flutter

  15. 15

    Reactjs: Dialog open is not working on button click

  16. 16

    dialog buttons with count-down for default button

  17. 17

    Android: Change a Button's text in a Dialog

  18. 18

    Function not responding to button click in a Google HTML Dialog

  19. 19

    jQuery: how to set selected in jQuery dialog?

  20. 20

    yacal.js next/back button removes jquery event listener

  21. 21

    How to set databaseInstanceName in Database Trace Listener from C# code?

  22. 22

    How do you set a Menu item listener (onMenuItemSelected) in a FragmentPagerAdapter?

  23. 23

    Android Custom View: How to set click listener on individual widgets

  24. 24

    Closing alert dialog in android on press of button for first time

  25. 25

    How do i bind action to a dialog "OK" button in Codename

  26. 26

    angular 8 material dialog close button with X top right

  27. 27

    jQuery UI Dialog Content being shown in button section

  28. 28

    NullPointerException when dialog positive button onClick calls interface

  29. 29

    How to show Application Launcher Dialog when do tap on button

ホットタグ

アーカイブ