• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm using Android Navigation Component for Navigation. I have a LoginFragment which has a button to transition to SignUpFragment. On clicking the button I'm getting this error.

java.lang.IllegalStateException: View android.support.v7.widget.AppCompatButton{49d9bd1 VFED..C.. ...P.... 201,917-782,1061 #7f090172 app:id/signUpLink} does not have a NavController set

Here is my nav_graph.xml

<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        app:startDestination="@id/loginFragment">
        <fragment
            android:id="@+id/loginFragment"
            android:name="org.fossasia.openevent.app.core.auth.login.LoginFragment"
            android:label="login_fragment"
            tools:layout="@layout/login_fragment">
            <action
                android:id="@+id/action_loginFragment_to_signUpFragment"
                app:destination="@id/signUpFragment" />

        </fragment>
    </navigation>

Here is the code in LoginFragment for Navigation -

binding.signUpLink.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_loginFragment_to_signUpFragment, null));

Here is extract from activity layout file for NavHostFragment -

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:name="android.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/main_navigation"
    app:defaultNavHost="true"/>
    • I also had got this issue. But in my case, the error is caused because I placed the Button wrongly in _activity.xml file at the same level with "androidx.navigation.fragment.NavHostFragment" fragment instead in _fragment.xml file. Now I moved the "Button" to _fragment.xml file and it's working. I'm not sure this can help you or not. Just sharing about the issue that I have been faced :-)

Actually, Navigation can't find NavController in FrameLayout. So replacing <FrameLayout> with <fragment> will make it work.

Add the following inside the <fragment> tag -

android:name="androidx.navigation.fragment.NavHostFragment"

After changes, it will look similar to this -

 <fragment
       android:id="@+id/fragment_container"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:layout_behavior="@string/appbar_scrolling_view_behavior"
       android:name="androidx.navigation.fragment.NavHostFragment"
       app:navGraph="@navigation/main_navigation"
       app:defaultNavHost="true"/>
  • 33
Reply Report
      • 1
    • @Peter I was able to solve this by upgrading gradle depedencies of material to implementation 'com.google.android.material:material:1.2.0-alpha04'.
      • 2
    • had an exception with FragmentContainerView, but works fine with fragment. Also when used FragmentContainerView it give lint warning that app:navGraph not set, but no such warning with fragment
      • 2
    • This works fine, but will give a lint warning on the use of the tag, stating that it should be replaced with a FragmentContainerView (although doing so will make your app crash).

Make sure that the home fragment in activity has name parameter set to:

android:name="androidx.navigation.fragment.NavHostFragment"

for example:

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

    <fragment
        android:id="@+id/navigationHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
        app:navGraph="@navigation/cycle_count_navigation" />

</RelativeLayout>
  • 8
Reply Report
    • a bit tricky this one, I honestly saw your answer but skipped it and tried other solutions and then when all else failed came back to this and it work!! just like that.

I've done some research and found this Issue also on Google. So here's the solution in Java:

NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.nav_host_fragment);
NavController navCo = navHostFragment.getNavController();
  • 6
Reply Report

Use the view of fragment such as onViewCreated

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val navController = Navigation.findNavController(view)

    binding.signUpLink.setOnClickListener {
            navController.navigate(R.id.action_loginFragment_to_signUpFragment)
    }
  • 3
Reply Report

I faced the same problem. So,instead of this,

binding.signUpLink.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_loginFragment_to_signUpFragment, null));

I used my NavHostFragment to find the NavHostFragment:

Button button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Fragment navhost = getSupportFragmentManager().findFragmentById(R.id.fragment2);
                NavController c = NavHostFragment.findNavController(navhost);
                c.navigate(R.id.firstFragment);


            }
        });

fragment2 is navhostfragmentID.

  • 3
Reply Report

A weird thing happens to me, below code snippet was working on normal flow from Fragment1 to fragment2, but after coming to fragment1 and on again navigate fragment2, this was throwing the "Navigation controller not set for the view" error.

    binding.ivIcon.setOnClickListener(v -> {
            Openfragment2(v);});

private void Openfragment2(View view) {
    Navigation.findNavController(binding.ivIcon).navigate(R.id.fragment2);

}

Here problem was in view, in findNavController need to pass the onclicked view.

private void Openfragment2(View view) {
    Navigation.findNavController(view).navigate(R.id.fragment2);

}
  • 0
Reply Report

i faced this issue just now, but i was sure about my code and then realized that i have changed the location of the fragment from under the main package to another folder

so i solved the issue with Build-> clean then Build ->make project to let the IDE to change its Directions class

  • 0
Reply Report