0

我有一个 Activity 并在那里使用 ViewBinding 没有问题。不是我创建了一个类似的 Fragment 并打算在那里使用 ViewBinding。不幸的是,我收到了错误消息,尽管我按照谷歌开发者页面中提到的关于 ViewBinding 的步骤(https://developer.android.com/topic/libraries/view-binding

在这里您可以看到 Java 文件:

package com.example.td.barapp;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.td.barapp.databinding.ActivitySelectionMenuBinding;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Menu_Fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Menu_Fragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;



    public Menu_Fragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Menu_Fragment.
     */
    // TODO: Rename and change types and number of parameters
    public static Menu_Fragment newInstance(String param1, String param2) {
        Menu_Fragment fragment = new Menu_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        binding.imageButtonCocktailsEn.setOnClickListener(this);
        binding.imageButtonCocktailsAlcfreeEn.setOnClickListener(this);
        binding.imageButtonLongdrinksEn.setOnClickListener(this);
        binding.imageButtonWhiskyEn.setOnClickListener(this);
        binding.imageButtonLiquorEn.setOnClickListener(this);
        binding.imageButtonBeerEn.setOnClickListener(this);
        binding.imageButtonSoftdrinksEn.setOnClickListener(this);
        binding.imageButtonHotDrinksEn.setOnClickListener(this);
    }


    private Menu_FragmentBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_menu_, container, false);

        //Include ViewBinding
        binding = Menu_FragmentBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        return view;

    }
}

我收到错误消息“无法解析符号 'Menu_FragmentBinding'”和“无法解析方法 'getRoot()'”。我不明白为什么我得到它们。你们中有人有想法吗?我会很感激每一条评论,我会感谢你的帮助。

4

1 回答 1

1

首先,不要在布局文件名中使用尾随下划线。删除尾随下划线fragment_menu_

为每个布局文件生成一个绑定类。默认情况下,类的名称是基于布局文件的名称,将其转换为 Pascal 大小写并为其添加 Binding 后缀。上面的布局文件名是activity_main.xml,所以对应的生成类是ActivityMainBinding.

所以如果你的布局文件的名字是fragemnt_menu.xml,那么对应生成的类就是FragmentMenuBinding

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Inflate using viewbinding
        binding = FragmentMenuBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }
于 2020-08-25T14:47:01.440 回答