Actually, to know the causes of such acts while sleeping we recommend that one visits a specialized physician or psychiatrist as this will surely help. It is beyond the specialization of an online Fiqh page to tell the causes of such things, and the best ways to avoid them.

With the above in mind, we advise to recite the last two verses from Surat Al-Baqarah (the Cow), as the Prophet, peace and blessings be upon him, said: “Whoever recites the last two verses from Surat Al-Baqarah at night, they will be sufficient for him.” (Narrated by Al-Bukhari, 3786; Muslim, 807).
Also, it is Sunnah to recite Surat Al-Ikhlas and Al-Mu`awwidhatayn before sleeping. `Aa’ishah, may Allah be pleased with her, narrates: “On going to bed, the Messenger of Allah, peace and blessings be upon him, would blow into his hands and recite Surat Al-Ikhlas and Al-Mu`awidhatyn.” Then he would wipe his hands over his face and his body, as far as they could reach.” `Aa’ishah continues: “When he fell ill, He would ask me to do that for him.” (Narrated by Al-Bukhari, 5416; Muslim, 2192)
Al-Bara’ Ibn `Aazib, may Allah be pleased with him, narrates: The Prophet said to me, “Whenever you go to bed, perform ablution like the one you perform for the prayer, lie on your right side and say, “Allahumma aslamtu wajhi ilaika, wa fawadtu ‘Amri ilaika, wa alja’tu Zahri ilaika raghbatan wa rahbatan ilaika. La Malja’a wa la manja minka illa ilaika. Allahumma amantu bikitabika-l-ladhi anzalta wa bina-biyika-l ladhi arsalta” (O Allah! I surrender to You and entrust all my affairs to You and depend upon You, fearing You, and reposing my hope in You. There is no fleeing from You, and there is no protection and safety except with You, O Allah! I believe in Your Book (the Qur’an) which You have revealed and in Your Prophet (Muhammad) whom You have sent). Then if you die on that very night, you will die as believer (in the religion of Islam). Let the aforesaid words be your last utterance (before sleep).” I repeated it before the Prophet and when I reached “Allahumma amantu bikitabika-l-ladhi anzalta (O Allah I believe in Your Book which You have revealed).” I said, “Wa-rasulika (and your Messenger).” The Prophet said, “No, say: ‘Wanabiyika-l-ladhi arsalta (Your Prophet whom You have sent), instead.”

Also on this issue, Abu Hurayrah was quoted as saying: “The Messenger of Allah, peace and blessings be upon him, used to tell us that when any one of us wanted to sleep, he should lie on his right side, then say ‘Allahumma Rabb al-samaawaati wa Rabb al-ard wa Rabb al-‘Arsh il-‘Azeem, Rabbana wa Rabba kulli shay’in, faaliq al-habb wa’l-nawa wa munazzil al-Tawraata wa’l-Injeela wa’l-Furqaan, a’oodhu bika min sharri kulli shay’in anta aakhidhun bi naasiyatihi. Allahumma anta al-awwal fa laysa qablaka shay’un, wa anta al-aakhir fa laysa ba’daka shay’un. Wa anta az-Zaahir fa laysa fawqaka shay’un wa anta al-baatin fa laysa doonaka shay’un. Iqdi ‘annaa al-dayna wa aghninaa min al-faqr (O Allah, Lord of the heavens and the earth, and Lord of the Mighty Throne, our Lord and Lord of all things, Splitter of the seed and the date stone, Revealer of the Tawraat (Torah), the Injeel (Gospel) and the Furqaan (Qur’an), I seek refuge in You from the evil of all things that You will seize by the forelock [i.e., have full control over them]. O Allah, You are the First and there is nothing before You; You are the Last and there is nothing after You. You are the Manifest (az-Zaahir) and there is nothing above You; You are the Hidden (al-Baatin) and there is nothing beyond You. Settle our debt and spare us from poverty).’” (Narrated by Muslim, 2713).
Abu Hurayrah (may Allah be pleased with him) was also quoted as saying: “Abu Bakr said: ‘O Messenger of Allah, teach me something that I can say in the morning and in the evening.’ He said: “Say, ‘Allahumma ‘Aalim al-ghaybi wa’l-shahaadah, Faatir al-samawaati wa’l-ard, Rabba kulli shay’in wa maleekahu, ashhadu an laa ilaaha illa anta. A’oodhi bika min sharri nafsi wa min sharr il-shaytaan wa sharakihi (O Allah, Knower of the seen and the Unseen, Creator of the heavens and the earth, Lord and Sovereign of all things, I bear witness that there is no god except You. I seek refuge in You from the evil of my own self and from the evil and traps of Satan).’ Say this in the morning and in the evening, and when you go to bed.’” (Narrated by al-Tirmidhi, 3392; Abu Dawood, 5067).