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>
2
Answers
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.
Just move package name from build.gradle app level to manifest.
from
to