首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >添加资产文件夹后导航中的“错误:包R不存在”:Android

添加资产文件夹后导航中的“错误:包R不存在”:Android
EN

Stack Overflow用户
提问于 2022-06-12 21:39:05
回答 2查看 830关注 0票数 6

自从向我的项目添加了一个资产文件夹之后,我现在得到:

代码语言:javascript
运行
复制
error: package R does not exist
"return new ActionOnlyNavDirections(R.id.action_newAlarmFragment_to_homeFragment);"

它来自于这个自动生成的代码:

代码语言:javascript
运行
复制
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本身来自于以下片段:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
?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>
EN

Stack Overflow用户

回答已采纳

发布于 2022-06-15 07:57:01

只需将包名从build.gradle应用程序级别移到清单。

从…

代码语言:javascript
运行
复制
android  { 
    namespace 'com.example.app' //remove this
}

代码语言:javascript
运行
复制
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"> //add this
票数 16
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72596066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档