I recently started working with Android Studio and I have a question.
Do you need to create a new activity for landscape mode?
I made my app in portrait mode with my MainActivity.java and layout/content_main.xml. I created a landscape variation ( land/content_main.xml ) and changed the layout accordingly. I added new buttons and referenced them in the MainActivity file. Everything works fine ( the new buttons can be clicked but do nothing of course ).
After I write a new setOnClickListener function for the buttons that appear only in the layound-land mode there are no errors but the app wont start. Thanks for the help.
PS: I checked activity_main.xml and it has both portrait and landscape mode - so that should not be the problem.
Asked
Active
Viewed 168 times
-1

Phantômaxx
- 37,901
- 21
- 84
- 115

Victor Burlacu
- 45
- 3
-
1Post your code in order to help you better. But if the button only appears in landscape mode, surely you are setting a `setOnClickListener()` on a null object. Since this button does not appears in portatrait the `findViewById()` on this button will return null. – Marc Estrada Jun 02 '18 at 10:43
-
`Do you need to create a new activity for landscape mode?` **NO**. Just put the landscape version of your layout in the `layout-land` folder. – Phantômaxx Jun 02 '18 at 10:58
1 Answers
0
Yes, it won't run in the portrait mode because you have made the two buttons extra and you are making the reference in the MainActivity.java using findViewById
and its getting null in the portrait mode so it work on the land mode and throw null pointer exception in the portrait mode.
Don't make extra Buttons in the land mode or you can make the two fragment and inflate them in the MainActivity.

Kartik Shandilya
- 3,796
- 5
- 24
- 42

vaibhav sharma
- 95
- 9