如何从使用 Fragment 的 ViewPager2 获得用户点击?

How can I get user clicks from a ViewPager2 that uses Fragment?

我期待用户点击 Fragment 中的 Button。我正在使用 ViewPager2 在不同的片段之间滑动。

在其中一个片段中,我有一个按钮,我想在其中设置一个 onClickListener,但是当我尝试在 onOcreateView 上设置该按钮的 onClickListener 时,出现此错误:

E/InputEventReceiver: Exception dispatching input event.
A/cifireca.words: java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    java_vm_ext.cc:570]   at android.view.View cifireca.words.FragmentStock.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) (FragmentStock.java:40)
    java_vm_ext.cc:570]   at void androidx.fragment.app.Fragment.performCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) (Fragment.java:2600)
    java_vm_ext.cc:570]   at void androidx.fragment.app.FragmentManagerImpl.moveToState(androidx.fragment.app.Fragment, int, int, int, boolean) (FragmentManagerImpl.java:881)
    java_vm_ext.cc:570]   at void androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(androidx.fragment.app.Fragment) (FragmentManagerImpl.java:1238)
    java_vm_ext.cc:570]   at void androidx.fragment.app.BackStackRecord.executeOps() (BackStackRecord.java:434)
    java_vm_ext.cc:570]   at void androidx.fragment.app.FragmentManagerImpl.executeOps(java.util.ArrayList, java.util.ArrayList, int, int) (FragmentManagerImpl.java:2079)
    java_vm_ext.cc:570]   at void androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(java.util.ArrayList, java.util.ArrayList, int, int) (FragmentManagerImpl.java:1869)
    java_vm_ext.cc:570]   at void androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(java.util.ArrayList, java.util.ArrayList) (FragmentManagerImpl.java:1824)
    java_vm_ext.cc:570]   at void androidx.fragment.app.FragmentManagerImpl.execSingleAction(androidx.fragment.app.FragmentManagerImpl$OpGenerator, boolean) (FragmentManagerImpl.java:1696)
    java_vm_ext.cc:570]   at void androidx.fragment.app.BackStackRecord.commitNow() (BackStackRecord.java:293)
    java_vm_ext.cc:570]   at void androidx.viewpager2.adapter.FragmentStateAdapter.placeFragmentInViewHolder(androidx.viewpager2.adapter.FragmentViewHolder) (FragmentStateAdapter.java:341)
    java_vm_ext.cc:570]   at void androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(androidx.viewpager2.adapter.FragmentViewHolder) (FragmentStateAdapter.java:276)
    java_vm_ext.cc:570]   at void androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(androidx.recyclerview.widget.RecyclerView$ViewHolder) (FragmentStateAdapter.java:67)

我看到确实是setOnClickListener引起的,但是我不知道怎么解决。

我实现 onClickListener 的方法如下:

public class FragmentStock extends Fragment {
   ...
   private Button buttonAccept;
   ...
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_stock, container, false);
       ...
       buttonAccept = viewGroup.findViewWithTag(R.id.button_stockAccept);
       buttonAccept.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               ... do whatever
           }
       });
       return viewGroup;
   }
...
}

使用 findViewById 而不是 findViewWithTag 我认为它应该可以解决您的问题。

buttonAccept = viewGroup.findViewById(R.id.button_stockAccept);