首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException in FrameLayout.onMeasure()

NullPointerException in FrameLayout.onMeasure()
EN

Stack Overflow用户
提问于 2014-08-14 21:55:59
回答 6查看 3.1K关注 0票数 10

编辑:由于我已经调试了这个程序并尝试了各种更改,我意识到我发布的原始代码并不真正相关,所以我删除了它:

我有一个Activity,当用户按下我在自定义布局中插入到ActionBar中的三个IconButton之一时,它可以在3个不同的IconButton之间切换,比如在各种模式之间切换:

当我第一次启动应用程序(已经卸载它),我默认为全球模式。全球模式在一个ViewPager中有几个片段,其中大部分是android.support.v4.app.ListFragment的子类。我正在创建主活动的onCreate方法中的所有8个选项卡。这如预期的那样工作,所有的东西都会正常加载。

当我进入“配置文件”模式时(通过单击Face图标按钮),我可以登录到应用程序中,并查看我的配置文件。这也像预期的那样工作,所有的东西都在正确地加载。

当我再次从Android推出APK时,因为我现在登录了(保存的首选项),它会默认我为“朋友提要”模式(两个人手握着手)。我可以将模式切换到配置文件,一切都如预期的那样工作,但是当我切换到Globe模式时,它在Android代码中崩溃了:

代码语言:javascript
复制
08-14 14:43:22.744  28804-29323/com.trover E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.trover, PID: 28804
    java.lang.NullPointerException
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:309)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

在过去两周内,我对该项目作了两项重大修改:

  1. 我已经删除了ActionbarSherlock库。我本来也希望删除support.v4库,直到我意识到我需要它来使用ViewPager。
  2. 我从Eclipse切换到Android,其中包括将所有文件移动到新的Android结构中。

今天早上,我重新安装了Eclipse并在删除ActionbarSherlock之后就开始了,并且无法重新创建这个崩溃。

我尝试过的事情:

  • 使用Gradle手动构建项目,并手动安装和运行它。当我做同样的步骤时,它会以同样的方式崩溃。
  • 通过调试器运行,并在崩溃时停止。第309行如下: 如果(mMeasureAllChildren = child.getVisibility() = GONE) {

在代码的这一点上,childnull,但是,对第296行的getChildCount()调用返回的值为2。我仍然不知道哪个视图在这一点上实际上崩溃了,在调试器中浏览这些变量时,任何变量都没有定义值。

  • 当您登录/退出时,溢出菜单中的项将被更改。当您单击顶部的IconButtons时,我调用IconButtons上的setBackgroundDrawable设置黄色高亮显示(如果不再是活动按钮,则设置null )。当我注释掉onCreateOptionsMenu中的代码和setHighlightedIconButton()中的代码时,崩溃不再发生。
  • 当我从ViewPager中删除所有片段时,崩溃不再发生。
  • 正如@kcoppock在下面提到的,可能是我在某个地方不恰当地膨胀了什么东西。在他链接的文章之后,我修改了我的应用程序中调用inflater.inflate(R.layout.somelayout, null)的每个调用,而不是inflater.inflate(R.layout.somelayout, viewGroupContainer, false),除了在选项卡上创建自定义视图(其中我不知道父选项卡下面的viewGroup )。在这种情况下,我将手动设置LayoutParameters。

在这一点上,我完全不知道下一步该做些什么。

更新:更改应用程序总是启动在全球模式防止崩溃,所以它似乎与切换到全球模式,如果我们没有开始它的活动。

下面是我为主要活动的viewPager构建Tabs的代码:

代码语言:javascript
复制
    // We HAVE to read this value out before creating the layout, or onTabSelected will set it back to zero
    final SharedPreferences prefs = getApplicationContext().getSharedPreferences(
            Const.Preferences.PREFS_FILE, Context.MODE_PRIVATE);
    boolean deniedLocationServices = prefs.getBoolean(Const.Preferences.LOCATION_SERVICES_DENIED, false);
    int previousTab = prefs.getInt(Const.Preferences.PREVIOUS_TAB, 0);

    setContentView(R.layout.main_browse);

    mFragmentManager = getSupportFragmentManager();

    mActionBar = getActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // these three lines hide the 'Trover' logo
    mActionBar.setDisplayHomeAsUpEnabled(false);
    mActionBar.setDisplayUseLogoEnabled(false);
    mActionBar.setIcon(new ColorDrawable(android.R.color.black));
    mActionBar.setCustomView(R.layout.action_bar_icons);
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
    final int numTabs = 8;

    TabbedBrowsePagerTab[] tabs = new TabbedBrowsePagerTab[numTabs];

    tabs[mAllTabPosition] = buildAllTab();
    tabs[mWhatsHotTabPosition] = buildWhatsHotTab();
    tabs[mWhatsNewTabPosition] = buildWhatsNewTab();
    tabs[mJumpToTabPosition] = buildJumpToTab();
    tabs[mFoodTabPosition] = buildFoodTab();
    tabs[mOutdoorTabPosition] = buildOutdoorTab();
    tabs[mArtTabPosition] = buildArtTab();
    tabs[mLatestTabPosition] = buildLatestTab();

    mPagerAdapter = new TabbedBrowsePagerAdapter(this, tabs);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(final int position) {
            try {
                mActionBar.setSelectedNavigationItem(position);
            } catch (final IllegalStateException e) {
                TroverApplication.logError(TAG, "IllegalArgumentsException setting tab position in PageChangeListener");
            }
        }
    });

    // Build the actual tabs on the actionbar
    for (int i = 0; i < tabs.length; i++) {
        mActionBar.addTab(mActionBar.newTab()
                .setTabListener(this));

        LayoutInflater inflater = LayoutInflater.from(this);
        View customView = inflater.inflate(R.layout.custom_action_bar_tabs, null);
        customView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        TextView tabCustom = (TextView) customView.findViewById(R.id.custom_action_bar_tab);
        tabCustom.setText(tabs[i].getTabTitle());
        tabCustom.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        tabCustom.setTypeface(TroverApplication.getDefaultFontBold());
        mActionBar.getTabAt(i).setCustomView(tabCustom);
    }

    // Now set up the button listeners for those icons
    mActionBar.getCustomView().findViewById(R.id.action_bar_friend_feed_button).setOnClickListener(this);
    mActionBar.getCustomView().findViewById(R.id.action_bar_me_button).setOnClickListener(this);
    mActionBar.getCustomView().findViewById(R.id.action_bar_nearby_button).setOnClickListener(this);

    mViewPager.setCurrentItem(previousTab);

    TroverLocationManager manger = TroverLocationManager.get();
    if (!manger.isNetworkLocationEnabled() && !deniedLocationServices) {
        showLocationServicesDialog();
    }

    if (AuthManager.get().isAuthenticated()) {
        mCurrentMode = Mode.NEWS_MODE; // <----- if I change this to GLOBE_MODE it doesn't crash
    } else {
        mCurrentMode = Mode.GLOBE_MODE;
    }
    validateView();

下面是main_browse.xml布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/main_camera_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/camera_floating" />

</RelativeLayout>

下面是setHighlightedIconButton函数:

代码语言:javascript
复制
private void setHighlightedIconButton() {
    ImageButton button;
    View iconContainer = mActionBar.getCustomView();

    PaintDrawable selectedBackground = new PaintDrawable(getResources().getColor(R.color.trover_selected_icon_background));

    button = (ImageButton) iconContainer.findViewById(R.id.action_bar_friend_feed_button);
    if (mCurrentMode == Mode.NEWS_MODE) {
        button.setBackgroundDrawable(selectedBackground);
        button.setClickable(false);
    } else {
        button.setBackgroundDrawable(null);
        button.setClickable(true);
    }

    button = (ImageButton) iconContainer.findViewById(R.id.action_bar_nearby_button);
    if (mCurrentMode == Mode.GLOBE_MODE) {
        button.setBackgroundDrawable(selectedBackground);
        button.setClickable(false);
    } else {
        button.setBackgroundDrawable(null);
        button.setClickable(true);
    }

    button = (ImageButton) iconContainer.findViewById(R.id.action_bar_me_button);
    if (mCurrentMode == Mode.PROFILE_MODE) {
        button.setBackgroundDrawable(selectedBackground);
        button.setClickable(false);
    } else {
        button.setBackgroundDrawable(null);
        button.setClickable(true);
    }
}

这是validateView()函数,它调用它:

代码语言:javascript
复制
/**
 * Handles transitions between different fragments by checking the current mode and authentication state.
 * This function can handle being called multiple times, and will always try to do the least work possible
 * each time.
 */
private void validateView() {
    invalidateOptionsMenu();
    setHighlightedIconButton();

    boolean authenticated = AuthManager.get().isAuthenticated();

    switch(mCurrentMode) {
        case GLOBE_MODE:
            // Note - we don't record a screen here because the tabs will do that
            removeMeFragment();
            removeNewsFragment();
            removeOnboardingFragment();
            mViewPager.setVisibility(View.VISIBLE);
            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            mPagerAdapter.madeVisible();
            break;
        case NEWS_MODE:
            if (mPreviousTabPosition == mJumpToTabPosition) {
                InputMethodManager imm = (InputMethodManager) getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
            }
            recordScreen(getCurrentModeTrackingString(), this);
            removeMeFragment();
            removeOnboardingFragment();
            if (mNewsFeedFragment == null) {
                mNewsFeedFragment = DiscoveryListFragment.newNewsFeedInstance();
                mFragmentManager.beginTransaction().add(android.R.id.content, mNewsFeedFragment).commit();
            }
            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            mViewPager.setVisibility(View.GONE);
            break;
        case PROFILE_MODE:
            if (mPreviousTabPosition == mJumpToTabPosition) {
                InputMethodManager imm = (InputMethodManager) getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
            }
            if (authenticated) {
                recordScreen(getCurrentModeTrackingString(), this);
                removeNewsFragment();
                removeOnboardingFragment();
                if (mProfileFragment == null) {
                    mProfileFragment = UserDetailFragment.newMeInstance();
                    mFragmentManager.beginTransaction().add(android.R.id.content, mProfileFragment).commit();
                }
            } else {
                recordScreen(getCurrentModeTrackingString() + "/onboarding", this);
                removeMeFragment();
                removeNewsFragment();
                if (mOnboardingFragment == null) {
                    removeOnboardingFragment();
                    mOnboardingFragment = new OnboardingFragment();
                    mFragmentManager.beginTransaction().add(android.R.id.content, mOnboardingFragment).commit();
                }
            }
            mViewPager.setVisibility(View.GONE);
            break;
        default:
            TroverApplication.logError(TAG, "Invalid mode!");
    }
}

下面是主要活动的onCreateOptionsMenu

代码语言:javascript
复制
public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    if (!AuthManager.get().isAuthenticated()) {
        MenuItem item = menu.findItem(R.id.menu_notifications);
        if (item != null) {
            item.setVisible(false);
        }
        item = menu.findItem(R.id.menu_recommended_users);
        if (item != null) {
            item.setVisible(false);
        }
    }
    return true;
}

这是FrameLayout.onMeasure()函数的一部分,它是Android 19源代码的一部分,它正在崩溃:

代码语言:javascript
复制
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int count = getChildCount();  <-- this is returning 2

    final boolean measureMatchParentChildren =
            MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
            MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
    mMatchParentChildren.clear();

    int maxHeight = 0;
    int maxWidth = 0;
    int childState = 0;

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (mMeasureAllChildren || child.getVisibility() != GONE) { <-- crash happens here
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            maxWidth = Math.max(maxWidth,
                    child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
            maxHeight = Math.max(maxHeight,
                    child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
            childState = combineMeasuredStates(childState, child.getMeasuredState());
            if (measureMatchParentChildren) {
                if (lp.width == LayoutParams.MATCH_PARENT ||
                        lp.height == LayoutParams.MATCH_PARENT) {
                    mMatchParentChildren.add(child);
                }
            }
        }
    }
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-08-21 21:03:58

最后,我向AOSP提交了一个针对这个问题的bug:

https://code.google.com/p/android/issues/detail?id=75056

我得到的(相当简洁)响应表明,在ViewPager.onMeasure()函数中,它试图执行任何挂起的Fragment事务。不幸的是,这听起来像是我显示/隐藏的其他片段干扰了这个过程,并导致了NPE。

他们鼓励的解决方法是在我的FragmentManager.executePendingTransactions()函数结束时调用validateView(),大概是完成我提交的片段事务,这样viewPager就不会受到它们的影响。

票数 1
EN

Stack Overflow用户

发布于 2014-08-15 18:11:43

代码语言:javascript
复制
View customView = inflater.inflate(R.layout.custom_action_bar_tabs, null);

这很可能是你问题的根源。如果传入null,充气器不知道要生成哪种类型的LayoutParams (它从父ViewGroup生成它们)。我相信这会生成默认的ViewGroup.LayoutParams,但可能它根本没有提供任何LayoutParams。

您应该将其替换为:

代码语言:javascript
复制
View customView = inflater.inflate(R.layout.custom_action_bar_tabs, parent, false);

其中parent是将添加customView的ViewGroup。如果您没有可用的父级,则可以手动设置一些自定义LayoutParams:

代码语言:javascript
复制
View customView = inflater.inflate(R.layout.custom_action_bar_tabs, null);
customView.setLayoutParams(new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
票数 1
EN

Stack Overflow用户

发布于 2014-08-19 14:00:06

问题2:

由于异常是在布局期间引发的,因此validateView方法中的两个调用看起来不确定。将这两个调用替换为:mViewPager.setVisibility(View.GONE);,改为mViewPager.setVisibility(View.INVISIBLE);

进入全局模式时,从消失到可见之间的转换是触发布局。从不可见到可见不会触发布局,这是NPE发生在哪里的一个很好的猜测。

更新。根据您的注释,让我们假设消失的->可见的转换只是在不同的布局中触发NPE (即在自定义操作栏中)。

通过转储示例应用程序的视图层次结构(Eclipse > DDMS >用于view的转储视图等级),我在操作栏中找到了一些FrameLayouts,这很有趣。当然,我的示例应用程序没有使用您的自定义视图,但是即使在操作栏的图标/主页部分,框架布局中也有一个ImageView

因此,尝试替换这段棘手的代码:

代码语言:javascript
复制
// these three lines hide the 'Trover' logo
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayUseLogoEnabled(false);
mActionBar.setIcon(new ColorDrawable(android.R.color.black));
mActionBar.setCustomView(R.layout.action_bar_icons);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME);

使用这个更简单的代码,应该以安全的方式完成相同的任务:

代码语言:javascript
复制
mActionBar.setCustomView(R.layout.action_bar_icons);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

如果不是这样的话,抛出您的实际视图并在您的自定义操作栏中研究FrameLayouts应该是有帮助的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25318191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档