Android Floating Action Button Lib Used

我在xml文件中创建了 float 操作菜单,并在此 float 操作菜单下创建了动态 float 操作按钮。我在每个FAB上设置图像位图。在某些特定情况下,我必须删除“ float 操作菜单”下的所有按钮。但是在removeView()方法上,当尝试在Null Pointer Exception方法中获取子对象的可见性时,我在子对象上获取了onMeasure()。下面是堆栈跟踪和我的代码。

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
        at com.getbase.floatingactionbutton.FloatingActionsMenu.onMeasure(FloatingActionsMenu.java:224)
        at android.view.View.measure(View.java:17565)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1263)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.widget.ScrollView.onMeasure(ScrollView.java:337)
        at android.view.View.measure(View.java:17565)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        at android.view.View.measure(View.java:17565)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17565)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
        at android.view.View.measure(View.java:17565)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2045)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1196)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1409)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Z

用于动态添加删除 float 操作按钮的代码:
       @Override
       public void onTap() {

                if (menuMultipleActions != null) {
                for (int i = 1; i <= mSelectedImages.size(); i++) {
                System.out.println("i value ==> " + i);
                menuMultipleActions.removeView(menuMultipleActions.getChildAt(i));
          }
         }
         mSelectedImages.clear();
      }


   @Override
     public void onPictureTaken(String filePath) {
          mSelectedImages.add(filePath);
          menuMultipleActions = (FloatingActionsMenu) findViewById(R.id.multiple_action);
          Snackbar.make(this.findViewById(android.R.id.content), "size = " + mSelectedImages.size(), Snackbar.LENGTH_SHORT).show();
      if (mSelectedImages.size() > 0) {
             for (int num = 0; num < mSelectedImages.size(); num++) {
                 floatingActionButton = new        com.getbase.floatingactionbutton.FloatingActionButton(getBaseContext());
                   floatingActionButton.setSize(com.getbase.floatingactionbutton.FloatingActionButton.SIZE_MINI);
                    floatingActionButton.setImageBitmap(getImageFromPath(mSelectedImages.get(num)));
                 floatingActionButton.setId(num);
                 menuMultipleActions.addButton(floatingActionButton);
           }
       }
    }

最佳答案

尝试使用menuMultipleActions.removeButton()而不是removeView()方法。在内部removeButton处理一些标志。

FloatingActionButton.java

07-25 21:12