0

I'm quiet new to android development. I've been trying to create a chat application with a navigation bar and tool bar. I used a template I found online to create the chat application and it's working. However when trying to add the tool bar manually I came to some problems. I found that the MainFragment was hard-coded onto the XML. This disallowed me to use the navigation bar (seeing that you can't replace a fragment if it's hard-coded onto the XML). So this lead me to following this: https://developer.android.com/training/basics/fragments/fragment-ui.html, as displayed below:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolBar = (Toolbar) findViewById(R.id.nav_act);
        setSupportActionBar(mToolBar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.navigation_header);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open, R.string.close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(this);

        //Set up Mainfragment
        if (findViewById(R.id.fragment_container) != null) {
            if (savedInstanceState != null) {
                return;
            }
            MainFragment firstFragment = new MainFragment();

            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
        }
    }

I removed the hard-coded MainFragment XML and replaced it with a RelativeLayout and gave it an id "Fragment_Container".

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_container"></RelativeLayout>

    <android.support.design.widget.NavigationView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/navigation_view"
        app:menu="@menu/menu_navigation"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

The problem is, when I try to run my code, I get this error:

 Process: com.github.nkzawa.socketio.androidchat, PID: 8429
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.github.nkzawa.socketio.androidchat/com.github.nkzawa.socketio.androidchat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                      at com.github.nkzawa.socketio.androidchat.MainActivity.onCreate(MainActivity.java:40)
                      at android.app.Activity.performCreate(Activity.java:6975)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6541) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

For reference, this is the MainFragment layout:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/message_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainFragment">

        <include layout="@layout/navigation_action" android:layout_width="match_parent"
        android:layout_height="wrap_content"></include>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/messages"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:scrollbars="vertical"
            android:scrollbarStyle="outsideOverlay"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <EditText
                android:id="@+id/message_input"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/prompt_message"
                android:imeActionId="@+id/send"
                android:imeActionLabel="@string/action_send"
                android:imeOptions="actionSend"
                android:inputType="text"
                android:maxLines="1"
                android:singleLine="true"
                tools:ignore="InvalidImeActionId" />
            <ImageButton
                android:id="@+id/send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_send"
                android:contentDescription="@string/action_send"/>

        </LinearLayout>

    </LinearLayout>

Styles.XML is:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@color/grey2</item>
    </style>
</resources>

I wasn't getting this error when I had the XML hard-coded. Does anybody understand why I'm getting this error? Thanks!

Skeol
  • 37
  • 7
  • please add your style.xml here – Ratilal Chopda Nov 13 '17 at 18:38
  • I added the style.xml. – Skeol Nov 13 '17 at 20:10
  • That `Activity` code expects a `Toolbar` to be in the `activity_main` layout. If that's what you want, then you'll need to move it there from wherever it is now, and structure your `DrawerLayout` similar to what is shown in [this answer](https://stackoverflow.com/a/26440880). If it's in the ``d layout in `MainFragment`'s layout, you can just move the `` to where it shows the `` in that example. – Mike M. Nov 13 '17 at 23:09

0 Answers0