App is crashing on orientation change - android

App is crashing on orientation change,fragment has Fragmentstatepager
Crash log:
java.lang.NullPointerException
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.ja va:569)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281)
Solution: I fixed the bug by replacing getChildManager() with getSupportManager()

Write following code in your Manifest file...
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
and write following code in YourActivity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Hope this helps you..

you can lock the orientation by adding the following code
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

You should read and learn the official tutorial Android-Google about Layout.
For have a good showing, you have to add your elements (Button, TextView..) on multiple Layout.
After your App will be automatically resize depending orientation and size of your phone.
Or you can just locked orientation in your Manifest app
android:screenOrientation="landscape" or "portrait"
Good Luck.
EDIT :
Official :
http://developer.android.com/guide/topics/ui/declaring-layout.html
An other :
http://code.tutsplus.com/tutorials/android-user-interface-design-layout-basics--mobile-3671

Related

How to keep android rotation landscape always

I want to keep landscape mode always.
I Lock screen to portrait mode on Nexus7, I set android:screenOrientation="sensorLandscape" and android:configChanges="orientation|keyboardHidden"in AndroidManifest.xml, when I go to other activity and back to this activity, then the screen will rotate portrait and rotate to landscape again (Didn't call onCreate method again), but I don't want to rotate the screen in any situations, how to fix it?
I think this is the answere you're looking for:
stackoverflow.com/a/2730894/2249774
For All your Activities in the manifest set
android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"
Example :-
<activity android:name="com.example.test.testActivity" android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:configChanges="orientation|keyboardHidden"">
<intent-filter>
<action android:name="com.example.test.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Repeat this for all activities used in the manifest..
Try editing your AndroidManifest.xml to look like this
<activity
android:name=".YOURACTIVITYNAME"
android:screenOrientation="landscape" />
In your AndroidManifest file, try typing in the following code:
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
example:
<activity
android:name="com.gaspar.slinfilipino.Quiz"
android:label="#string/title_activity_quiz"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="landscape"
android:parentActivityName="com.gaspar.slinfilipino.SignLanguageMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gaspar.slinfilipino.SignLanguageMenu" />
</activity>

How to avoid force close on mobile phone orientation change?

my android application crashes and shows force close when i tilt the mobile phone. is there any suggestions that i can remove such problem? By the way i am developing a LBS application which uses the google maps (MapView).
I have a splash screen and then shown a ListActivity as below :
<activity android:name=".Splash" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Home" android:label="#string/app_name" android:configChanges="orientation" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="com.nepways.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
what is wrong with my declaration, the splash screen loads and list activity is also whown correctly but when i change the orientation the application is closed. Please help me.
When you change your orientation the activity is restarted
look at the logcat to see what exception causes your application to FC
You can change your manifest to disable activity restarting by customizing configChanges, if you stil have the problem override onConfigurationChanged() to fix what is generating this exception (like initialize something that may cause a nullpointer)
First of all as it is said the app is restarting (or as it is said in reference redrawing) each time when you change orientation.
To block it in your manifest file in activity you have to put this line which describes the activity orientation:
<activity android:name=".Splash"
android:label="#string/app_name"
android:screenOrientation="portrait">...
And about this buttons, or keymaps. Telling the truth Ive got thise warnings sometimes but it doesnt change anything in my app. So first change the orientation settings and then it should work correct.
If you want use other screen orientation remember to check here:
http://developer.android.com/guide/topics/manifest/activity-element.html
If you want to avoid restarting your activity on orientation change, you can put this in the activity in the manifest file:
android:configChanges="orientation"
Otherwise, we will need to see more code, and a stacktrace.

Android app closes when the orientation is changed

I want my app to run in the portrait view so I made the following changes in the android manifest file.
android:screenOrientation="portrait"
It stays in the portrait view for a second and then it closes.. I had assumed this is the only change i had to make..but i also added this but it isnt working
android:configChanges="orientation"
Am i missing something?
<activity android:name=".Menu" android:label="COMEDY TRIVIA"
android:screenOrientation="portrait" android:configChanges="orientation"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="nik.trivia.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Try this adding android:configChanges to your AndroidManifest file in the activity tag
<activity android:name=".Fourth" android:label="Fourth" android:configChanges="orientation|keyboardHidden"></activity>
Hope this works for you.

android:configChanges seems to be ignored

I try to prevent that onCreate() is called when rotating the device. To reach this goal I followed the documentation and added
<activity android:name=".TabActivity"
android:label="#string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
to the manifest. After that I inserted this to the Activity's source code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
But it seems like Android ignores this method. A breakpoint which I set here is never reached.
What's my fault here? Is this a problem when using tabs?
Regards!
Try this:
android:configChanges="orientation|keyboardHidden"

Prevent screen rotation on Android

I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
Add
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
to the <activity> element/s in
the manifest and you're done.
You can follow the logic below to prevent auto rotate screen while your AsyncTask is running:
Store your current screen orientation inside your activity using getRequestedOrientation().
Disable auto screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR).
Run/execute your AsyncTask.
At the end of your AsyncTask restore your previous orientation status using setRequestedOrientation(oldOrientation).
Please note that there are several ways to access Activity (which runs on UI thread) properties inside an AsyncTask. You can implement your AsyncTask as an inner class or you can use message Handler that poke your Activiy class.
The easiest way I found to do this was to put
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
within onCreate, just after
setContentView(R.layout.activity_main);
so...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
<activity
...
...
android:screenOrientation="portrait">
Rather than going into the AndroidManifest, you could just do this:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
#Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
The only drawback here is that it requires API level 18 or higher. So basically this is the tip of the spear.
Activity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="QRCodeActivity" android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
User "portrait" in your AndroidManifest.xml file might seem like a good solution. But it forces certain devices (that work best in landscape) to go into portrait, not getting the proper orientation. On the latest Android version, you will get wearing an error. So my suggestion it's better to use "nosensor".
<activity
...
...
android:screenOrientation="nosensor">
Add the following to your AndroidManifest.xml
[ app > src > main > AndroidManifest.xml ]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
The following attribute on the ACTIVITY in AndroidManifest.xml is all you need:
android:configChanges="orientation"
So, the full activity node would be:
<activity android:name="Activity1"
android:icon="#drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
...
...
...
}
Prevent Screen Rotation just add this following line in your Manifests.
<activity
android:name=".YourActivity"
android:screenOrientation="portrait" />
This works for me.
android:screenOrientation="portrait"
on application tag <application
and
<activity
...
...
android:screenOrientation="locked">
If you are using Android Developer Tools (ADT) and Eclipse
you can go to your AndroidManifest.xml --> Application tab --> go down and select your activity. Finally, select your preferred orientation.
You can select one of the many options.
You have to add the following code in the manifest.xml file.
The activity for which it should not rotate, in that activity add this element
android:screenOrientation="portrait"
Then it will not rotate.
You can try This way
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itclanbd.spaceusers">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Login_Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Use AsyncTaskLoader to keep your data safe even if the activity changes, instead of using AsyncTask that is a better way to build apps than preventing screen rotation.

Categories

Resources