错误:找不到符号 import androidx.navigation.fragment.navArgs

huangapple go评论77阅读模式
英文:

Error: cannot find symbol import androidx.navigation.fragment.navArgs

问题

我正在进行来自网站https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#8 的"使用Java构建您的第一个Android应用"教程。一切都正常,直到我到达第8步,该步骤是导入androidx.navigation.fragment.navArgs,但是我遇到了一个错误:

import androidx.navigation.fragment.navArgs;

我该如何修复这个错误?这是什么意思?

英文:

I was doing the "Build Your First Android App in Java" from the website https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#8.
Everything worked until I got to step 8, which was to import androidx.navigation.fragment.navArgs, but I got an error:

import androidx.navigation.fragment.navArgs;

How do I fix it? What does it mean?

答案1

得分: 5

这是一个与codelab相关的错误,从代码实验的Kotlin版本中提取,该版本使用by navArgs() Kotlin属性委托来检索参数(如在使用Safe Args指南中所述)。

在Java中不需要这样做,因为你可以使用SecondFragmentArgs.fromBundle(getArguments())方法来检索参数类。

因此,你可以直接跳过那个导入:它在任何地方都没有被使用,可以安全地忽略掉。

英文:

This is an error with the codelab, pulled in from the Kotlin version of the code lab which uses the by navArgs() Kotlin property delegate to retrieve the arguments (as explained in the Using Safe Args guide).

This isn't needed in Java, as you use the SecondFragmentArgs.fromBundle(getArguments()) method to retrieve the arguments class.

Therefore, you can just skip that import: it isn't used anywhere and can safely be ignored.

答案2

得分: 1

以下是翻译好的内容:

如果您正在按照在Java中构建您的第一个应用程序,并且遇到了此错误,那么请尝试注释删除以下导入:

import androidx.navigation.fragment.navArgs;
英文:

if you are following building your first app in java and you encountered this error,than try commenting or deleting following import

import androidx.navigation.fragment.navArgs;

答案3

得分: 0

你在构建脚本部分有两个 build.gradle 文件。其中一个是项目范围的文件,另一个是模块范围的文件。

在项目的那个文件中,你必须在 dependencies 部分添加以下代码:

    dependencies {

        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha04"
    }

而在另一个文件中,也就是用于模块的 build.gradle 文件,你需要添加以下代码(通常与其他相似的行在顶部):

apply plugin: 'androidx.navigation.safeargs.kotlin'

另外,鉴于你正在开始学习,我强烈建议你学习 Kotlin 而不是 Java。Kotlin 是谷歌支持的开发 Android 应用的新语言。

如果这解决了你的问题,请告诉我。

英文:

You have two build.gradle in your build scripts section. One of them is your project wide file and the other is your module one.

In your project one, you have to have this line inside your depencies:

    dependencies {

        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha04"
    }

in your other file, the build.gradle that is for your module, you have to add this following line (it is usually on top of it with the other lines alike):

apply plugin: 'androidx.navigation.safeargs.kotlin'

On other note, as you are starting to learn, I strongly recommend that you learn Kotlin instead of Java. Kotlin is the newly Google supported language to develop Android Apps.

Let me know if it fixes your issue.

答案4

得分: -1

这是Java教程中的错误,因为较新的Android Studio版本,在Kotlin教程中,不会出现这些问题。

尝试打开“SecondFragment.java”文件,并在该行添加以下内容:
@SuppressLint("StringFormatInvalid" //解决格式问题的行。

onViewCreated方法之前添加,使其如下所示:

@SuppressLint("StringFormatInvalid")
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Integer count = SecondFragmentArgs.fromBundle(getArguments()).getMyArg();
    String countText = getString(R.string.random_heading, count);
    TextView headerView = view.getRootView().findViewById(R.id.textview_header);
    headerView.setText(countText);
    Random random = new java.util.Random();
    Integer randomNumber = 0;

    if (count > 0) {
        randomNumber = random.nextInt(count + 1);
    }

    TextView randomView = view.getRootView().findViewById(R.id.textview_random);
    randomView.setText(randomNumber.toString());

    view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NavHostFragment.findNavController(SecondFragment.this)
                    .navigate(R.id.action_SecondFragment_to_FirstFragment);
        }
    });

在我告诉你的那行代码之后的所有其他代码与教程中的代码相同(我仅为了确保将其放在这里),希望对你有所帮助;)。

英文:

This is an error with the Java tutorial because of the newer Android Studio versions, on the Kotlin one, it doesn't give you that problems.

try to go to the "SecondFragment.java" file and add this in the line:
@SuppressLint("StringFormatInvalid" //line that evades the formatting problem.

before the onViewCreated method, so it would end like this:

@SuppressLint("StringFormatInvalid")
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Integer count = SecondFragmentArgs.fromBundle(getArguments()).getMyArg();
    String countText = getString(R.string.random_heading, count);
    TextView headerView = view.getRootView().findViewById(R.id.textview_header);
    headerView.setText(countText);
    Random random = new java.util.Random();
    Integer randomNumber = 0;

    if (count > 0) {
        randomNumber = random.nextInt(count + 1);
    }

    TextView randomView = view.getRootView().findViewById(R.id.textview_random);
    randomView.setText(randomNumber.toString());

    view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NavHostFragment.findNavController(SecondFragment.this)
                    .navigate(R.id.action_SecondFragment_to_FirstFragment);
        }
    });

All the other code after that line I told you is the same as the tutorial (I put it just in case tho), hope it helps ;).

huangapple
  • 本文由 发表于 2020年5月31日 07:02:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/62109659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定