-1

I have a fragment where I'm trying to set up the title and back button but it keeps on throwing a null pointer exception error.

I have tried.

How to get Toolbar from fragment?

Getting Toolbar in Fragment

Using Toolbar with Fragments

The toolbar xml, inside of fragment_job_apply.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/border_toolbar"
    app:popupTheme="@style/StanbyTheme"
    app:subtitleTextColor="@color/black"
    app:titleTextColor="@color/black"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@color/black"
            android:textSize="20sp"
           />

        <LinearLayout
            android:id="@+id/toolbar_search_container"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/search_box_background"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:padding="6dp"
            android:visibility="gone">

            <ImageView
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:background="@drawable/stanby_ic_drawer_search"/>

            <TextView
                android:id="@+id/toolbar_search_text_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:ellipsize="end"
                android:maxLines="1"
                android:layout_marginStart="5dp"
                android:layout_marginLeft="5dp"
                android:textColor="@color/gray3"
                android:textSize="16sp"
                android:padding="2dp"
                android:text="@string/search_box_default_text" />

        </LinearLayout>



        <FrameLayout
            android:id="@+id/toolbar_notifications"
            style="@style/AppTheme.FrameLayout.Fill"
            android:layout_width="wrap_content"
            android:gravity="center_vertical|right"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:visibility="gone">

            <ImageView
                style="@style/AppTheme.ImageView"
                android:src="@drawable/stanby_ic_drawer_info"
                android:layout_gravity="center" />
        </FrameLayout>



        <!-- こんな用法は、アクションバーじゃないけど、工数ないので、既存を改修 -->
        <!-- 求人詳細アイコン -->
        <FrameLayout
            android:id="@+id/toolbar_link"
            style="@style/AppTheme.FrameLayout.Fill"
            android:layout_width="wrap_content"
            android:gravity="center_vertical|right"
            android:paddingLeft="13dp"
            android:paddingRight="13dp"
            android:visibility="gone">

            <ImageView
                style="@style/AppTheme.ImageView"
                android:src="@drawable/v_u_ic_jobs_grey"
                android:layout_gravity="center" />
        </FrameLayout>

        <!-- 縦点3つアイコン -->
        <FrameLayout
            android:id="@+id/toolbar_link2"
            style="@style/AppTheme.FrameLayout.Fill"
            android:layout_width="wrap_content"
            android:gravity="center_vertical|right"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:visibility="gone">

            <ImageView
                style="@style/AppTheme.ImageView"
                android:src="@drawable/v_u_ic_more_vert_grey"
                android:layout_gravity="center" />
        </FrameLayout>

        <!-- 保存アイコン -->
        <FrameLayout
            android:id="@+id/toolbar_link_text_layout"
            style="@style/AppTheme.FrameLayout.Fill"
            android:layout_width="wrap_content"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:gravity="center_vertical|right"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:visibility="gone">

            <TextView
                android:id="@+id/toolbar_link_text_text"
                style="@style/AppTheme.TextView.Medium.Gray3"
                android:layout_gravity="center"
                android:textColor="@color/black" />
        </FrameLayout>



    </LinearLayout>

</android.support.v7.widget.Toolbar>

The Class

public class JobApplyFragment extends BaseRxFragment {
   private final JobApplyFragment self = this;

   protected Toolbar mToolbar;

   @Override
   public View onCreateView(
           LayoutInflater inflater,
           ViewGroup container,
           Bundle savedInstanceState) {
       Bundle arguments = getArguments();
       jobMasterData = (JobMasterData) 
       arguments.getSerializable(BUNDLE_JOB_MASTER_DATA);
       contentType = arguments.getString(BUNDLE_CONTENT_TYPE);
       mUrl = jobMasterData.documentUrl;
       mIsAts = jobMasterData.isAts();

        View view = inflater.inflate(R.layout.fragment_job_apply, container, false);
        // neither works
        // mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        // mToolbar = (android.support.v7.widget.Toolbar) view.findViewById(R.id.toolbar);
        // mToolbar.setTitle("my title");

        return view;
    }
}

Question: How can I set the title and back button on this?

Trevor Wood
  • 2,347
  • 5
  • 31
  • 56
  • You've got two lines trying to find the `Toolbar`; one in the `Activity`'s layout, and one in the `Fragment`'s `View`. The second one supersedes the first. Where exactly is the `Toolbar`? – Mike M. Nov 29 '17 at 01:43
  • @MikeM. I edited it with a little more information. I don't want to include the whole class since it's verbose. Let me know if you need any other information that I may have forgotten – Trevor Wood Nov 29 '17 at 01:51
  • Where exactly is the `` with ID `toolbar`? – Mike M. Nov 29 '17 at 01:55
  • @MikeM. I uploaded the xml file with toolbar inside of it. – Trevor Wood Nov 29 '17 at 02:06

1 Answers1

1

1.Add theme of your app

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

2.Add xml code like this

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent" > 
</android.support.v7.widget.Toolbar>
  • You should add setSupportActionBar method .

  • And remove the mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); in your code .

TRY

Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
// edited here 
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setText("my title");
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42