9

I don't know what's wrong, whenever I launch my app on an Emulator and I switch to landscape mode, it crashes. But it work fine in portrait mode. Please what can i do to solve this. Thanks in advance! here's my codes...

MainActivity.class

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.actgeek.android.myapplication.R;

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MAFragment mfrag = new MAFragment();
        FragmentTransaction FT = getFragmentManager().beginTransaction();
        FT.replace(R.id.fragment, mfrag);
        FT.commit();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This Fragment displays in the activity_main layout

MAFragment.class

public class MAFragment extends ListFragment {

    public MAFragment() {
    }


   @Override
           public void onActivityCreated(Bundle savedInstanceState){
       super.onActivityCreated(savedInstanceState);

       String topics[] = Mylists.TOPICS;

       MyArrayAdapter adapter = new MyArrayAdapter(getActivity(),
               topics);

        setListAdapter(adapter);

       getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
           public void onItemClick(AdapterView<?> parent, View view, int
                   position, long id) {

               Intent intent = new Intent(getActivity(), SubTopicsActivity.class);
               intent.putExtra("index", position);
               startActivity(intent);
           }
       });


   }
}

activity_main layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main"
    tools:context="com.actgeek.android.studypack.MainActivity">


<fragment android:id="@+id/fragment"
          android:name="com.actgeek.android.studypack.MAFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>

Here's the error i'm getting whenever the app crashes.

12-19 11:08:18.383 28644-28644/com.actgeek.android.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actgeek.android.myapplication/com.actgeek.android.studypack.MainActivity}: java.lang.NullPointerException
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2360)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412)
                                                                                       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3919)
                                                                                       at android.app.ActivityThread.access$700(ActivityThread.java:162)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                                       at android.os.Looper.loop(Looper.java:194)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5388)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                                    Caused by: java.lang.NullPointerException
                                                                                       at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:94)
                                                                                       at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:87)
                                                                                       at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:77)
                                                                                       at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                                                                                       at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
                                                                                       at com.actgeek.android.studypack.MainActivity.onCreate(MainActivity.java:28)
                                                                                       at android.app.Activity.performCreate(Activity.java:5141)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412) 
                                                                                       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3919) 
                                                                                       at android.app.ActivityThread.access$700(ActivityThread.java:162) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                                                       at android.os.Looper.loop(Looper.java:194) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5388) 
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                                                                                       at dalvik.system.NativeStart.main(Native Method) 
devmike01
  • 1,971
  • 1
  • 20
  • 30
  • There is no Toolbar in that layout, so it's throwing the NPE when you try to call `setSupportActionBar()` with null. – Mike M. Dec 19 '15 at 10:17
  • see here http://stackoverflow.com/questions/7139488/androidconfigchanges-orientation-does-not-work-with-fragments – Mohammad Tauqir Dec 19 '15 at 10:17
  • @MikeM. I'm using Android Studio 1.5, and the toolbar is automatically created by the IDE. Its located in the activity_main. – devmike01 Dec 19 '15 at 10:28
  • It's not in the `activity_main` you've posted. If you mean you've got separate layouts for portrait and landscape, the Toolbar needs to be in both of them. – Mike M. Dec 19 '15 at 10:31
  • add this line in your xml file may be help you – Amit Basliyal Dec 19 '15 at 10:31
  • @MikeM. the 'activity_main' layout displays the toolbar in my 'content_main.xml', i don't know why but its the default code by Android Studio. – devmike01 Dec 19 '15 at 11:00
  • And where is `content_main` included in the layout you've posted, or loaded in the code you've shown? – Mike M. Dec 19 '15 at 11:02

3 Answers3

13

See if you are missing this in the activity declaration in your manifest

android:configChanges="orientation|screenSize|keyboardHidden"
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
4

check if you are not missing this property, while declaring in AndroidManifest.

android:configChanges="orientation|keyboardHidden"

updated :

<activity
            android:name=".SlideNewActivity"
            android:configChanges="orientation|keyboardHidden"
            android:launchMode="singleTask" />
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • The code works but it only make the screen static. It doesn't allow the screen to rotate. Anyways thanks for your help. – devmike01 Dec 19 '15 at 11:04
  • android:configChanges="orientation" , if you add this than it will not throw error in landscape mode. – Amit Vaghela Dec 19 '15 at 11:06
  • @DonMichaelGbenga to allow it to rotate you need to remove `android:screenOrientation="portrait"` from the code above. – Viral Patel Dec 19 '15 at 11:08
2

Add this Lines in to your activity in .manifest

        <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden"
        />

Also Add this override method in to your MainActivity

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
}

That's it.

Kishan Soni
  • 816
  • 1
  • 6
  • 19
  • will you please tell me had you add this method in your activity and after it which issue are you still facing ? – Kishan Soni Dec 19 '15 at 12:58