XML not switching when orientation changes

Initially my app was crashing when I would change the orientation but then I added the following line in manifest file


then error was resolved but I reliased that this line doesn’t allow switching of xml file when I change orientation so what should I do? Any help ?

If the app handled rotation just fine until now, there is probably something wrong with your landscape layout. Either the layout XML is malformed, or it is missing some IDs that your activity expects to find. If you still have a “previous” button from an earlier challenge, you will need to create it here.

Can you post the stack log from this crash?

Thanks for the reply I was finally able to solve the problem, I realised that I had to handle orientation myself with method onConfigutationChanged()

I strongly recommend not using the configChanges attribute. With that setting, you are telling Android not to destroy your activity across rotation and that you are handling rotation yourself. As a general practice, you should not do this. There are only a few cases where this setting is a good idea.

Like Nitrodon said, you have a problem with your layout file and this configChanges attribtue it just covering up that problem (but the problem is still there). Post your crash logs here and I can help you troubleshoot.

Here is the log

 Process: com.example.asd.quizap, PID: 32687
Unable to start activity ComponentInfo{com.example.asd.quizap/com.example.asd.quizap.QuizAct}:
Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

This is shown in logcat if I don’t put configChanges, please help and do tell which are the cases in which setting this is a good idea and what is the right way to make app compatible with orientation changes and remember the state as well

The stacktrace here says that you tried to call setOnClickListener on a null object. This means that one of your findViewById calls is returning a null value. This could be because you have a widget in the portrait layout and not in the landscape layout. You can use the debugger to pinpoint exactly where this happens.

Also, my recommendation to you is that you should never use the configChanges attribute. When you do this, you are just covering up a problem that will still happen when the system has to recreate your Activity due to low-memory (you “fix” the problem on rotation but you aren’t fixing it completely).

You can use the onSaveInstanceState mechanism to save your state across activity death and recreation. Keep working through the book and practice these concepts. I know this stuff can be confusing and complicated, but you will be a much better Android developer when you understand it.

Okay thanks for the reply, I will look into the problem, thanks for this
book, one of the best Android books out there (the only one I am hooked to
:grinning:). Good job, sir!