自从向我的项目添加了一个资产文件夹之后,我现在得到:
error: package R does not exist
"return new ActionOnlyNavDirections(R.id.action_newAlarmFragment_to_homeFragment);"
它来自于这个自动生成的代码:
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);
}
}
我尝试过清理和重建该项目,并尝试了“使缓存失效并重新启动”,如注释中所建议的那样。
在这里看其他已回答的问题,似乎是导致这一问题的原因之一,但我什么也找不到。
NavDirection本身来自于以下片段:
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>
发布于 2022-06-15 07:57:01
只需将包名从build.gradle应用程序级别移到清单。
从…
android {
namespace 'com.example.app' //remove this
}
至
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"> //add this
https://stackoverflow.com/questions/72596066
复制相似问题