Orientation change Crash Application

18,389

Solution 1

android:configChanges="keyboardHidden|orientation"

add this in your manifest file in your activity.

Solution 2

<activity android:name=".SignUp"
android:configChanges="keyboardHidden|orientation|screenSize">

Add this line to all your activities to avoid crash with orientation changes. This will avoid the activity being loaded again and again on orientation changes.


For a little more information what you actually do with configChanges here the link to the Android documentation.

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

Note: Using this attribute should be avoided and used only as a last resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

And how to handle configuration changes you can find here.

Share:
18,389
Bhim Prasad Ale
Author by

Bhim Prasad Ale

Updated on June 28, 2022

Comments

  • Bhim Prasad Ale
    Bhim Prasad Ale almost 2 years

    I am using fragments and when i change the orientation the application crash. Here is the log cat:

    05-29 05:56:52.158: E/AndroidRuntime(1428): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.bajraregistertesteclipse/com.example.bajraregistertesteclipse.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.os.Looper.loop(Looper.java:137)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at java.lang.reflect.Method.invoke(Method.java:511)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    05-29 05:56:52.158: E/AndroidRuntime(1428):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    05-29 06:17:02.864: E/AndroidRuntime(1554): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bajraregistertesteclipse/com.example.bajraregistertesteclipse.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
    05-29 06:17:02.864: E/AndroidRuntime(1554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    05-29 05:56:52.158: E/AndroidRuntime(1428): Caused by: java.lang.IllegalStateException: Fragment com.example.bajraregistertesteclipse.SecondFragment did not create a view.
    

    Here is my First_Test_Fragment class

    package com.example.bajraregistertesteclipse;
    
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class First_Test_Fragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.first_test_fragment,container,false);
        Button btnLogin=(Button)view.findViewById(R.id.btnLogin);
    
        btnLogin.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                FirstFragment firstFragment=new FirstFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.detailFragment, firstFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
    
            }
    
        });
        Button btnSignUp=(Button)view.findViewById(R.id.btnSignUp);
        btnSignUp.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                SignUp signup=new SignUp();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.detailFragment, signup);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
    
    
    
            }
    
        });
    
    
    
    
        return view;
    }
    
    
    
    }
    
    
    
    }
    

    Here is my FirstFragment Class

    package com.example.bajraregistertesteclipse;
    
    import android.app.Fragment;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class FirstFragment extends Fragment {
    LoginDataBaseAdapter loginDataBaseAdapter;
    EditText loginTestUser,loginTestPassword;
    String userName,password,confirmpassword;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    
           View view = inflater.inflate(R.layout.firstfragment, container, false);
    
    
          loginTestUser=(EditText)view.findViewById(R.id.editTextUserNameToLogin);
          loginTestPassword=    (EditText)view.findViewById(R.id.editTextPasswordToLogin);
           Button btnLogin = (Button) view.findViewById(R.id.buttonSignIn); 
    
            btnLogin.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                //  Toast.makeText(getActivity(),"BttonLoginPressed",Toast.LENGTH_SHORT).show();
    
                    // get The User name and Password
                     userName=loginTestUser.getText().toString();
                     password=loginTestPassword.getText().toString();
    
                  // fetch the Password form database for respective user name
                        String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
    
                        // check if the Stored password matches with  Password entered by user
                        if(password.equals(storedPassword))
                        {
                            Toast.makeText(getActivity(), "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
    
                            // Intent intent = new Intent(getActivity(), Admin_Page.class);
                           //     getActivity().startActivity(intent);
    
                            Intent open=new Intent("com.example.bajraregistertesteclipse.ADMIN_PAGE");
    
                            getActivity().startActivity(open);
                            // startActivity(open);
    
                        }
                        else
                        {
                            Toast.makeText(getActivity(), "User Name or Password does not match", Toast.LENGTH_LONG).show();
                        }
    
                }
    
            });
    
       return view;
    }
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        loginDataBaseAdapter=new LoginDataBaseAdapter(getActivity());
         loginDataBaseAdapter=loginDataBaseAdapter.open();
    }
    

    }

    My Mainfest.xml is :

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.bajraregistertesteclipse"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="16" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    
       <activity  
           android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.bajraregistertesteclipse.MAINACTIVITY" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
        <activity android:name=".SignUp"></activity>
         <activity android:name=".Admin_Page">
    
         </activity>
    
    </application>
    

    Now my problem is Application crash when orientation change and When click to the button new activity Admin_Page should be initiatied but application crash.

    Thank YOu in Advance!!!

  • Bhim Prasad Ale
    Bhim Prasad Ale about 11 years
    ya thanks...why my application crash when i do Intent intent = new Intent(getActivity(), Admin_Page.class); getActivity().startActivity(intent);
  • Mahesh
    Mahesh about 11 years
    What is the error that you are getting? I mean error in logcat
  • Mahesh
    Mahesh about 11 years
    You are welcome. @Rohan Ale Please accept any answer that you find as correct so that this question wont remain unanswered.. :)
  • Bhim Prasad Ale
    Bhim Prasad Ale about 11 years
    how can we destroy the previous activity. i.e when i sucessfully login then my new Activity start but when i press back button of moible i will directed towards my login page again which i want to destroy it. can you help me out ?
  • Mahesh
    Mahesh about 11 years
    use finish(); before starting new activity
  • Bhim Prasad Ale
    Bhim Prasad Ale about 11 years
    when i use finish(); ERROR: The method finish() is undefined for the type new View.OnClickListener(){
  • Mahesh
    Mahesh about 11 years
  • Mahesh
    Mahesh about 11 years
    If it is a fragment then we need to use getActivity().finish(); otherwise activity_class_name.finish();
  • Jithu
    Jithu almost 11 years
    Do you know any other method to manage this problem?? If you are using webview, css, etc related things, you cant use this method, because, you need to rearrange when orientation changes.
  • Filip Luchianenco
    Filip Luchianenco almost 7 years
    It's useless to answer in this form. Explain what you mean and how that affects the app and how it solves the described problem.
  • Crapo Wolf
    Crapo Wolf about 6 years
    This is the solution. You had to add this on your activity in manifest.
  • EpicPandaForce
    EpicPandaForce about 4 years
    This is most likely going to crash after process death, see stackoverflow.com/questions/49046773/…
  • EpicPandaForce
    EpicPandaForce about 4 years
    This is most likely going to crash after process death, see stackoverflow.com/questions/49046773/…
  • EpicPandaForce
    EpicPandaForce about 4 years
    This is most likely going to crash after process death, see stackoverflow.com/questions/49046773/…