使用 ViewBinding 获取空对象异常,而在使用 FindViewById 时一切正常

Getting a null object exception with ViewBinding while everyhting works fine when using FindViewById

在我的片段代码中,我试图将点击侦听器附加到此按钮,以便我可以使用它在导航图中导航。

当使用视图绑定时,应用程序崩溃并出现空对象异常,使用按 ID 查找视图修复了该问题,

我想知道怎么了!

java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.example.teleport.databinding.FragmentLoginBinding.registerLink' on a null object reference

这是我得到的错误,这是片段代码

public class LoginFragment extends Fragment implements View.OnClickListener {

// 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;

private FragmentLoginBinding binding;

private NavController navController;
private TextView registerLink;

public LoginFragment() {
    // 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 LoginFragment.
 */
// TODO: Rename and change types and number of parameters
public static LoginFragment newInstance(String param1, String param2) {
    LoginFragment fragment = new LoginFragment();
    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);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_login, container, false);
}

@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    navController = Navigation.findNavController(view);
    //registerLink = (TextView) view.findViewById(R.id.register_link);
    //registerLink.setOnClickListener(this);
    registerLink = (TextView) binding.registerLink;
    registerLink.setOnClickListener(this);
}




@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.register_link:
            navController.navigate(R.id.nav_register);
            break;
        }
    }
}

您需要在 onCreateView() 方法中创建绑定实例。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // create instance of binding here
    binding = FragmentLoginBinding.inflate(inflater, container, false);    
    return binding.getRoot();
}

此外,明确表示您不需要获取视图的引用,例如

registerLink = (TextView) binding.registerLink;
registerLink.setOnClickListener(this);

您可以直接使用绑定实例作为

binding.registerLink.setOnClickListener(this);

有关详细信息,请查看文档 here