找不到 FirstFragmentDirections 和 SecondFragmentArgs。

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

cannot find FirstFragmentDirections & SecondFragmentArgs

问题

public class SecondFragment extends Fragment {
    Integer myArg = SecondFragmentArgs.fromBundle(getArguments()).getMyArg();

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.random_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView showCountTextView = view.getRootView().findViewById(R.id.textview_first);
            int currentCount = Integer.parseInt(showCountTextView.getText().toString());
            FirstFragmentDirections.ActionFirstFragmentToSecondFragment action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(currentCount);
            NavHostFragment.findNavController(FirstFragment.this).navigate(R.id.action_FirstFragment_to_SecondFragment);
        }
    });

    view.findViewById(R.id.toast_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast myToast = Toast.makeText(getActivity(), "Hello toast!", Toast.LENGTH_SHORT);
            myToast.show();
        }
    });

    view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countMe(view);
        }
    });
}
英文:

From: https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#8

AndroidStudio 3.6.2

Last tasks in the step-by-step guide are producing the following errors:

>cannot find a symbol variable SecondFragmentArgs
package FirstFragmentDirections does not exist
cannot find symbol variable FirstFragmentDirections

Here is a sample of the code from SecondFragment.java:

public class SecondFragment extends Fragment {
    Integer myArg = SecondFragmentArgs.fromBundle(getArguments()).getMyArg();
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

Here is the code from FirstFragment.java:

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.random_button);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView showCountTextView = view.getRootView().findViewById(R.id.textview_first);
            int currentCount = Integer.parseInt(showCountTextView.getText().toString());
            FirstFragmentDirections.action_FirstFragment_to_SecondFragment action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(currentCount);
            NavHostFragment.findNavController(FirstFragment.this).navigate(R.id.action_FirstFragment_to_SecondFragment);
        }
    });

    view.findViewById(R.id.toast_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast myToast = Toast.makeText(getActivity(), "Hello toast!", Toast.LENGTH_SHORT);
            myToast.show();
        }
    });

    view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countMe(view);
        }
    });

}
}

答案1

得分: 3

我遇到了相同的问题,原来codelab在Build.gradle文件中引用了SafeArgs的alpha版本。要解决这个问题,只需在顶层的Build.gradle文件中包含最新的classpath。

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.3.0";
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version";
    }
}

此外,不要忘记将插件应用于你的应用或模块Build.gradle文件:

对于Java,使用以下代码:

apply plugin: "androidx.navigation.safeargs"

对于Kotlin,使用以下代码:

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

编辑:为了完整起见,你还需要重新构建Gradle,以便添加FirstFragmentDirections生成的类。可以通过在工具菜单中选择Build > Make Project来完成。

如果上述方法不起作用,可以参考下面的官方文档进行额外的故障排除。

参考文档:

https://developer.android.com/guide/navigation/navigation-pass-data

https://developer.android.com/jetpack/androidx/releases/navigation#safe_args

英文:

I faced the same issue, it turned out the codelab was referencing an alpha version of SafeArgs in the Build.gradle file. To fix it, just include the latest classpath in your top level Build.gradle file.

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.3.0"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

Also, do not forget to apply plugin to your app or module's Build.gradle file:

apply plugin: "androidx.navigation.safeargs" if using Java or

apply plugin: "androidx.navigation.safeargs.kotlin" for Kotlin

EDIT: For the sake of completeness, you also need to Rebuild Gradle for the FirstFragmentDirections generated class to be added. That can be done by choosing Build > Make Project in the tool menu.

Consult with the official docs below, as they offer additional troubleshooting if the above is not working.
Referenced Docs

https://developer.android.com/guide/navigation/navigation-pass-data

https://developer.android.com/jetpack/androidx/releases/navigation#safe_args

答案2

得分: 1

  1. “Project Structure -> Gradle Version” 的最低要求是 6.1.1。
  2. 在最右边打开你的 Gradle 侧边栏(或者按下两次 Shift 键 -> Gradle)。
  3. 点击“切换离线模式”按钮,确保它处于关闭状态。
  4. 这可能会开始一个大规模的 Gradle 下载,等待直到完成。
  5. 前往 “Build -> Make”。
  6. 不要忘记同步。
英文:
  1. Project Structure -> Gradle Version needs to be at least 6.1.1.
  2. Open your Gradle sidebar on the far right (or double shift -> Gradle).
  3. Click the "Toggle Offline Mode" so it's off.
  4. This could start a massive Gradle download, wait until finished.
  5. Go to Build -> Make
  6. Don't forget to sync.

huangapple
  • 本文由 发表于 2020年4月7日 14:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61073842.html
匿名

发表评论

匿名网友

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

确定