0

I cant solve this simple problem.
i have Main activity,it has a toolbar and a fragment.

Main Activity xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.dimfcompany.macapp2.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBarTest"
        android:titleTextColor="@color/macYellow"
        android:title="dsfsdf"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/macRed">


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

    <fragment
        android:id="@+id/testfrag"
        android:name="com.dimfcompany.macapp2.fragtest1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></fragment>

</LinearLayout>

MainActivity Java

public class MainActivity extends AppCompatActivity {

Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar=(Toolbar)findViewById(R.id.toolBarTest);
    toolbar.setTitle("BLDFLDSF");
}

public void setToolBarText(String text)
{
    toolbar.setTitle("dsfsdfsdf");
}
}

Setting title in MainActivity.java onCreateMethod works.
Then i want my title to change if fragment content changes.
In my fragment I have the onViewCreated() method and I'm trying to change the title from this method.

public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
    ((MainActivity)getActivity()).setToolBarText("FragmentTitle");

    toolbar=(Toolbar) view.findViewById(R.id.toolBarTest);
    toolbar.setTitle("FragmentTitle");
}

I tried changing it directly and changing with calling method setToolBarText in MainActivity.

I always have an exception

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference

What i am doing wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bios90
  • 801
  • 2
  • 14
  • 26
  • 1
    See https://stackoverflow.com/questions/27100007/set-title-intoolbar-from-fragment-in-android .. Why are you getting `Toolbar` in fragment' – ADM Mar 17 '18 at 17:17
  • Please don't cast the Activity from the Fragment. Use an attachment interface, as shown in this post https://stackoverflow.com/questions/24777985/how-to-implement-onfragmentinteractionlistener – OneCricketeer Mar 17 '18 at 17:19

1 Answers1

1

Unless you're loading an XML view in the Fragment also with android:id="@+id/toolBarTest", then this first line will return a null view, and the second throws an error.

toolbar=(Toolbar) view.findViewById(R.id.toolBarTest);
toolbar.setTitle("FragmentTitle");

You're already attempting to use the Activity method to set the title, so these aren't needed

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245