onMeasure()中的子项的空指针异常:futuresimple / floating-action-button

时间:2015-10-17 09:22:53

标签: android nullpointerexception floating-action-button

Android Floating Action Button Lib Used

我在xml文件中创建了浮动操作菜单,并在此浮动操作菜单下创建了动态浮动操作按钮。我在每个FAB上设置图像位图。在某些特定情况下,我必须删除浮动操作菜单下的所有按钮。但是在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

动态添加移动操作按钮的代码:

       @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);
           }
       }
    }

2 个答案:

答案 0 :(得分:3)

尝试使用menuMultipleActions.removeButton()代替removeView()方法。在removeButton内部操纵一些标志。

FloatingActionButton.java

答案 1 :(得分:2)

给出的代码有两个问题。首先,正如Viswanath使用removeView()指出上面总是崩溃(无论循环)

menuMultipleActions.removeView(menuMultipleActions.getChildAt(0));

第二个问题是循环将始终崩溃,因为当您删除每个按钮时,menuMultipleActions中的视图数减少一个。对此的解决方案要么是向后循环,要么总是删除第0个孩子,例如

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

虽然我可能更倾向于使用menuMultipleActions.getChildCount()作为查找按钮数量的更安全的方法。

更通用的解决方案,它将在循环中工作或通过id删除任何单个按钮,将使用findViewById在删除它之前找到该按钮。例如

FloatingActionButton fab = (FloatingActionButton) menuMultipleActions.findViewById(ButtonId);
if (fab != null) menuMultipleActions.removeButton(fab);

如果为FloatingActionButton设置了标签,您也可以使用findViewByTag,因为该实现使用标签作为标记。