android studio navigtion not woring when movieng with fragments in java

so i wanted to create a footer navigtion on the main activity and a fragment view above it but now when i try to use the normal navigtion with the navigtion grafh it just crashes. mainActivity java code:

package com.example.mywatchlist;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    

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


        Button homeBtn = findViewById(R.id.homeBtn);
        Button profileBtn = findViewById(R.id.profileBtn);
        Button socialBtn = findViewById(R.id.theSocialSearchBtn);
        Button contentBtn = findViewById(R.id.contentContentSearchBtn);



        homeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                homeFragment home = new homeFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,home).commit();
            }
        });

        profileBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userProfileFragment profile = new userProfileFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,profile).commit();
            }
        });

        socialBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                socialSearchFragment social = new socialSearchFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,social).commit();
            }
        });

        contentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                contentSearchFragment contentSearch = new contentSearchFragment();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView,contentSearch).commit();
            }
        });


    }

}

main activity.xml:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="632dp"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation" />

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/homeBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Go tonHome"
            android:textSize="12dp" />

        <Button
            android:id="@+id/profileBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="MynProfile"
            android:textSize="12dp" />

        <Button
            android:id="@+id/theSocialSearchBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Search fornPeople"
            android:textSize="12dp" />

        <Button
            android:id="@+id/contentContentSearchBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Search fornContent"
            android:textSize="12dp" />
    </LinearLayout>
</LinearLayout>

and now it crashes when i try to move in this fragment: fragment to move from java code:

package com.example.mywatchlist;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link contentSearchFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class contentSearchFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public contentSearchFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment contentSearchFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static contentSearchFragment newInstance(String param1, String param2) {
        contentSearchFragment fragment = new contentSearchFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content_search,container, false);
        // Inflate the layout for this fragment


        Button searchBtn = view.findViewById(R.id.contentContentSearchBtn);

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Navigation.findNavController(view).navigate(R.id.action_contentSearchFragment_to_contentFragment);

            }
        });


        return view;
    }
}

and this is the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".contentSearchFragment" >

    <TextView
        android:id="@+id/contentSearchTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#C4FF80"
        android:gravity="center"
        android:text="Content search"
        android:textSize="30dp" />

    <Button
        android:id="@+id/contentSearchAddNewContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add new content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFEB3B"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/contentSearchBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#C8C8C8"
            android:ems="10"
            android:hint="Search for content"
            android:inputType="textPersonName"
            android:minHeight="48dp" />

        <Button
            android:id="@+id/contentContentSearchBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Search" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="502dp">

        <TextView
            android:id="@+id/contentSearchData"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="testntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntesttestntestntestntestntestntestXXXXXXXXXXXXXXXXXXntestntestntestntestntestntestntestntestntestntestntestnXXXXXXXXXXXXXXXXX testntestnXXXXXXXXXXXXXXXXntestntestntestntestntest" />
    </ScrollView>

</LinearLayout>

and this is the navigation xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigation"
    app:startDestination="@id/startFragment">
    <fragment
        android:id="@+id/startFragment"
        android:name="com.example.mywatchlist.startFragment"
        android:label="fragment_start"
        tools:layout="@layout/fragment_start" >
        <action
            android:id="@+id/action_startFragment_to_logInFragment"
            app:destination="@id/logInFragment" />
        <action
            android:id="@+id/action_startFragment_to_registerFragment"
            app:destination="@id/registerFragment" />
    </fragment>
    <fragment
        android:id="@+id/logInFragment"
        android:name="com.example.mywatchlist.logInFragment"
        android:label="fragment_log_in"
        tools:layout="@layout/fragment_log_in" >
        <action
            android:id="@+id/action_logInFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
    </fragment>
    <fragment
        android:id="@+id/registerFragment"
        android:name="com.example.mywatchlist.registerFragment"
        android:label="fragment_register"
        tools:layout="@layout/fragment_register" >
        <action
            android:id="@+id/action_registerFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
    </fragment>
    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.mywatchlist.homeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/userProfileFragment"
        android:name="com.example.mywatchlist.userProfileFragment"
        android:label="fragment_user_profile"
        tools:layout="@layout/fragment_user_profile" />
    <fragment
        android:id="@+id/contentFragment"
        android:name="com.example.mywatchlist.contentFragment"
        android:label="fragment_content"
        tools:layout="@layout/fragment_content" />
    <fragment
        android:id="@+id/contentSearchFragment"
        android:name="com.example.mywatchlist.contentSearchFragment"
        android:label="fragment_content_search"
        tools:layout="@layout/fragment_content_search" >
        <action
            android:id="@+id/action_contentSearchFragment_to_contentFragment"
            app:destination="@id/contentFragment" />
    </fragment>
    <fragment
        android:id="@+id/socialSearchFragment"
        android:name="com.example.mywatchlist.socialSearchFragment"
        android:label="fragment_social_search"
        tools:layout="@layout/fragment_social_search" >
        <action
            android:id="@+id/action_socialSearchFragment_to_userProfileFragment"
            app:destination="@id/userProfileFragment" />
    </fragment>
</navigation>

crashing:

    --------- beginning of crash
2022-07-01 14:32:49.986 25363-25363/com.example.mywatchlist E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mywatchlist, PID: 25363
    java.lang.IllegalStateException: View com.google.android.material.button.MaterialButton{3bedc11 VFED..C.. ...P.... 921,5-1440,173 #7f080237 app:id/contentContentSearchBtn} does not have a NavController set
        at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
        at com.example.mywatchlist.contentSearchFragment$1.onClick(contentSearchFragment.java:73)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2022-07-01 14:32:50.027 25363-25363/com.example.mywatchlist I/Process: Sending signal. PID: 25363 SIG: 9

thank you to all the helpers

Leave a Comment