添加工具栏

时间:2016-07-01 12:03:12

标签: android android-fragments android-studio

在尝试添加已在主设计xml文件以外的布局文件中定义的工具栏时获取NullPointerException。我一直收到以下错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.Toolbar.findViewById(int)' on a null object reference

这是我的代码

public class History_List_Details_Fragment extends Fragment {

Histroy_List_Model histroyListModel;
public History_List_Details_Fragment() {

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    histroyListModel = getArguments().getParcelable("historydetail");
    String title=histroyListModel.getShared_title();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    super.onCreate(savedInstanceState);


    View rootView = inflater.inflate(R.layout.history_list_details_fragment, container, false);
    Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.app_bar);
    initializeListnersForToolbar(toolbar);
    initializeViews(rootView);

    return rootView;
}

private void initializeViews(View rootView) {
    TextView titleText = (TextView) rootView.findViewById(R.id.txtTitle);
    titleText.setText(histroyListModel.getShared_title());

    TextView messageTExt = (TextView) rootView.findViewById(R.id.txtMessage);
    titleText.setText(histroyListModel.getShared_message());


}

private void initializeListnersForToolbar(Toolbar toolbar){
    ((TextView) toolbar.findViewById(R.id.tool_bar_text_head)).setText(getResources().getString(R.string.friends));

    ((ImageView) toolbar.findViewById(R.id.tool_bar_back_arrow)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Fragment fragment=new History_List_Fragment();

            Utilities.getInstance(getActivity()).change_HomPage_Fragment(
                    fragment, "History_List_Fragment", getActivity());
        }
    });
}

}

History_List_Details_Fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:orientation="horizontal"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Title"
            android:id="@+id/txtTitle"
            android:layout_gravity="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Message"
            android:id="@+id/txtMessage"
            android:layout_gravity="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Duration"
            android:id="@+id/txtDuration"
            android:layout_gravity="center" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/lstMembers"></ListView>

    </LinearLayout>

</LinearLayout>

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

在“history_list_details_fragment”中添加ID为“app_bar”的工具栏

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="68dp"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/txtTitle"
        android:layout_gravity="center" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:gravity="center" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Message"
        android:id="@+id/txtMessage"
        android:layout_gravity="center" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Duration"
        android:id="@+id/txtDuration"
        android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/lstMembers"></ListView>

</LinearLayout>

答案 1 :(得分:1)

错误清楚地表明。在history_list_details_fragment.xml里面没有Toolbar。尝试添加它。

或者更有可能工具栏位于Activity布局中,因此请在Activity内进行初始化。