0

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...

  • Please post a complete example which illustrates what you are trying to do. Code should be everything necessary to reproduce the problem but not include anything that is irrelevant to the question. Specifically, all code must be in a class and a method, just like Java requires. – Code-Apprentice Feb 05 '17 at 17:09
  • I suggest that you create two different fragments rather than one fragment with two different layouts. – Code-Apprentice Feb 05 '17 at 17:10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Code-Apprentice Feb 05 '17 at 17:10

1 Answers1

0

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

Debdeep
  • 752
  • 10
  • 22
  • yes, i get that. How do i prevent that when i dismiss –  Feb 05 '17 at 17:08
  • Instead of switching, you may keep the same code inside that particular layout file and manage their visibilities. That would be much easier. – Debdeep Feb 05 '17 at 17:12
  • that would be my last resort as the other layout may contain several element and managing all of that will be herculean –  Feb 05 '17 at 17:15
  • They may look herculean but both of'em would still have a root tag, eh ? :D – Debdeep Feb 05 '17 at 17:17
  • i thought only 1 root tag is allowed per xml @Debdeep –  Feb 05 '17 at 17:22
  • 1
    I know that, what I meant to say - keep the root as it is. Rest of the code would be treated as two parts with two main tags with nested tags under them. You would simply manage the visibility on those main tags. – Debdeep Feb 05 '17 at 17:24