简体   繁体   中英

How to switch between layouts in custom dialog fragment (Android)

i'ld like to know how i can switch my dialogs layout file without creating a separate one.

I have a custom dialog fragment i use for connecting to bluetooth devices in my app. It pops up a list of devices and i connect to a device of my choosing.

I have two xml layouts i want to use with this dialog fragment: - The first one holds the listview for devices i want to connect to - The other houses and image view

When i connect to a device, i want to switch the layout from the list to the one that houses the imageview. Some where in my code, i have a variable that checks the connection status.

if i'm connected, i switch to the other layout like this:

getDialog().setContentView(R.layout.xml2);

and it works but then when i want to show the dialog again, i get this error.

Attempt to invoke virtual method 'void android.app.Dialog.setContentView(int)' on a null object reference

In my onCreateView method, i'm check my connection state.

if (connected) {

   return inflater.inflate(R.layout.xml2, container, false);

 } else {

   return inflater.inflate(R.layout.xml1, container, false);
 }

I know the error has to do with the changing getDialog().setContentView when state changed to connected. I'm thinking about how to revert back to the default view on dismiss so the onCreateView can take effect. If there is another method to doing this, i'ld like to hear about it. Any Ideas?

Thanks in advance...

Your getDialog() method returns null. You may want to have a look at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM