11

Since adding an Assets folder to my project I now get:

error: package R does not exist
"return new ActionOnlyNavDirections(R.id.action_newAlarmFragment_to_homeFragment);"

which is from this auto generated code:

import androidx.annotation.NonNull;
import androidx.navigation.ActionOnlyNavDirections;
import androidx.navigation.NavDirections;

public class SetNewAlarmFragmentDirections {
  private SetNewAlarmFragmentDirections() {
  }

  @NonNull
  public static NavDirections actionNewAlarmFragmentToHomeFragment() {
    return new ActionOnlyNavDirections(R.id.action_newAlarmFragment_to_homeFragment);
  }
}

I have tried cleaning and rebuilding the project and tried "Invalidate caches and restart" as suggested in the comments

Looking through other answered questions here it seems it can be an import of R. somewhere causing this, but I can't find anything..

The NavDirection itself comes from this fragment:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.pleavinseven.alarmclockproject.alarmmanager.AlarmManager
import com.pleavinseven.alarmclockproject.data.model.Alarm
import com.pleavinseven.alarmclockproject.data.viewmodel.AlarmViewModel
import com.pleavinseven.alarmclockproject.databinding.FragmentSetNewAlarmBinding
import com.pleavinseven.alarmclockproject.util.TimePickerUtil
import java.util.*


class SetNewAlarmFragment : Fragment() {

    private val timePickerUtil = TimePickerUtil()
    lateinit var binding: FragmentSetNewAlarmBinding
    private lateinit var alarmViewModel: AlarmViewModel


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        binding = FragmentSetNewAlarmBinding.inflate(inflater, container, false)

           binding.fragmentCreateAlarmRecurring.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { _, isChecked ->
        if (isChecked) {
            binding.fragmentCreateAlarmRecurring.visibility = View.VISIBLE
        } else {
            binding.fragmentCreateAlarmRecurring.visibility = View.GONE
        }
    })

    alarmViewModel = ViewModelProvider(this)[AlarmViewModel::class.java]


    binding.fragmentBtnSetAlarm.setOnClickListener(View.OnClickListener { _ ->
        scheduleAlarm()
        Navigation.findNavController(requireView())
            .navigate(com.pleavinseven.alarmclockproject.R.id.action_newAlarmFragment_to_homeFragment)
    })
    return binding.root


}

nav 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/alarm_nav"
app:startDestination="@id/homeFragment">

<fragment
    android:id="@+id/homeFragment"
     android:name="com.pleavinseven.alarmclockproject.fragments.HomeFragment"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" >
    <action
        android:id="@+id/action_homeFragment_to_newAlarmFragment"
        app:destination="@id/newAlarmFragment" />
    <action
        android:id="@+id/action_homeFragment_to_updateFragment"
        app:destination="@id/updateFragment" />
</fragment>
<fragment
    android:id="@+id/newAlarmFragment"
    android:name="com.pleavinseven.alarmclockproject.fragments.SetNewAlarmFragment"
    android:label="NewAlarmFragment" >
    <action
        android:id="@+id/action_newAlarmFragment_to_homeFragment"
        app:destination="@id/homeFragment" />
</fragment>
<fragment
    android:id="@+id/updateFragment"
    android:name="com.pleavinseven.alarmclockproject.fragments.UpdateFragment"
    android:label="UpdateFragment" >
    <action
        android:id="@+id/action_updateFragment_to_homeFragment"
        app:destination="@id/homeFragment" />
    <argument
        android:name="currentAlarm"
        app:argType="com.pleavinseven.alarmclockproject.data.model.Alarm" />
</fragment>
Joe Pleavin
  • 436
  • 1
  • 4
  • 21
  • Did you try "Invalidate caches and restart"? – Gavin Wright Jun 12 '22 at 22:00
  • Just tried it now.. didn't change anything. – Joe Pleavin Jun 12 '22 at 22:10
  • And what about if you remove this Assets folder you just added? I'm thinking you may have added it incorrectly and thus Android Studio can't compile the app's resources. – Gavin Wright Jun 12 '22 at 22:16
  • Also changes nothing. Maybe the assets folder is a red herring and something else is wrong, but that's where i have first noticed it. – Joe Pleavin Jun 12 '22 at 22:23
  • Compare your `findNavController()` usage against mine. You're doing some things differently: https://github.com/gavingt/upcoming-games/blob/d59dd2afd2ef7448ca2e826b0514e7db05195538/app/src/main/java/com/gavinsappcreations/upcominggames/ui/list/ListFragment.kt#L48 – Gavin Wright Jun 12 '22 at 22:28
  • I changed mine to follow the same structure as yours and then tried "invalidate caches and restart" again.. still nothing – Joe Pleavin Jun 12 '22 at 22:53
  • What about other types of resources? Try removing the navigation code from this Fragment and see if it will recognize a String resource. – Gavin Wright Jun 12 '22 at 22:59
  • even removing the navigation code form this Fragment does nothing.... just looking at the problem a little further though, *none* of the navigation parts are working now. – Joe Pleavin Jun 12 '22 at 23:07
  • In that case, I'd probably just start a new project and copy everything over (if that's not too much trouble). – Gavin Wright Jun 12 '22 at 23:09
  • It's a lot to move over, but if I have no other option.. I just cant understand what is causing this, and AS does nothing to point it out really – Joe Pleavin Jun 12 '22 at 23:13
  • Can you post your navigation graph XML code as well? Just update the OP to include it. – Gavin Wright Jun 12 '22 at 23:29
  • updated with the navigation graph XML – Joe Pleavin Jun 12 '22 at 23:57
  • Are you using Safeargs? If so, are you sure that you've set it up correctly? – Gavin Wright Jun 13 '22 at 00:10
  • yeah I'm using SafeArgs. The app has been working for months now up until today. Nothing has changed with navigation. – Joe Pleavin Jun 13 '22 at 00:16
  • sometimes invalidate cache + restart doesn't work for me either. Try deleting your *generated* `.gradle` and `.idea` folders and rebuild your project – Nick Borisenko Jun 13 '22 at 04:49
  • before I delete something by accident here.. how do you know exactly which ones are generated? – Joe Pleavin Jun 13 '22 at 12:30

2 Answers2

35

Just move package name from build.gradle app level to manifest.

from

android  { 
    namespace 'com.example.app' //remove this
}

to

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"> //add this
mitua1221
  • 473
  • 3
  • 6
1

The problem seems to be that AS is recommending that people move their package name to build.gradle rather than Android Manifest which is causing a bug in navigation.

Joe Pleavin
  • 436
  • 1
  • 4
  • 21
  • This link appears to be incorrect, or I don't understand the connection you're making. – Saul Aryeh Kohn Feb 09 '23 at 15:40
  • 1
    @SaulAryehKohn that was absolutely an incorrect link. I have no idea how I didn't notice it and nobody pointed it out for over a year, but it's gone now. If I find a relevant one, I'll edit it again later. – Joe Pleavin Feb 09 '23 at 15:55