编辑:当我对此进行调试并尝试了各种更改时,我意识到我发布的原始代码并不真正相关,所以我删除了它:
我有一个 Activity
可以在 3 个不同的 Fragment
之间切换s 当用户按下 3 个 IconButton
之一时s 我已经在自定义布局中插入了 ActionBar,比如在各种模式之间切换:
当我第一次启动应用程序(已将其卸载)时,我默认为 Globe 模式。 Globe 模式在 ViewPager
中有几个 fragment , 这些 fragment 中的大多数是 android.support.v4.app.ListFragment
的子类.我在主 Activity 的 onCreate 方法中创建了所有 8 个选项卡。这按预期工作,一切正常加载。
当我进入“个人资料”模式(通过单击人脸图标按钮)时,我可以登录应用程序并查看我的个人资料。这也按预期工作,一切正常加载。
当我再次从 Android Studio 推送 APK 时,由于我现在已登录(保存的首选项),它默认我为“ friend 提要”模式(两个人手牵手)。我可以将模式切换到 Profile,一切都按预期工作,但是当我切换到 Globe 模式时,它在 Android 代码中崩溃:
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)
在过去的两周里,我对项目进行了两次重大更改:
今天早上我重新安装了 Eclipse 并在删除 ActionbarSherlock 后立即进入了这一点,并且无法重新创建此崩溃。
我尝试过的事情:
if (mMeasureAllChildren || child.getVisibility() != GONE) {
此时代码中,
child
是 null
,然而,调用 getChildCount()
在第 296 行返回值 2。我仍然不知道此时哪个 View 实际崩溃了,当我在调试器中浏览它们时,任何变量中都没有定义值。IconButton
s 在上面,我调用 setBackgroundDrawable
在 IconButtons 上设置黄色突出显示(或设置 null
如果它不再是 Activity 按钮)。当我注释掉 onCreateOptionsMenu
中的两个代码时和 setHighlightedIconButton()
中的代码,崩溃不再发生 ViewPager
中删除所有 fragment 时,崩溃不再发生 inflater.inflate(R.layout.somelayout, null)
改为 inflater.inflate(R.layout.somelayout, viewGroupContainer, false)
,除非我在选项卡上创建自定义 View (我不知道 viewGroup 是它们的父级)。在这种情况下,我手动设置 LayoutParameters。 在这一点上,我完全不知道接下来要尝试什么。
更新:将应用程序更改为始终以 Globe 模式启动可以防止崩溃,因此如果我们没有启动它,它似乎与切换到 Globe 模式有关。
这是我为主 Activity 的 viewPager 构建选项卡的代码:
// 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 布局:
<?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 函数:
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() 函数:
/**
* 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
对于主要 Activity :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 API 19 源代码的一部分的函数:@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);
}
}
}
}
最佳答案
View customView = inflater.inflate(R.layout.custom_action_bar_tabs, null);
这可能是您的问题的根源。如果你传入
null
,inflater 不知道要生成什么类型的 LayoutParams(它从父 ViewGroup 生成它们)。我相信这会生成默认的 ViewGroup.LayoutParams,但也许它根本不提供任何 LayoutParams。您应该将其替换为:
View customView = inflater.inflate(R.layout.custom_action_bar_tabs, parent, false);
在哪里
parent
是 customView
所在的 ViewGroup将被添加。如果您没有可用的父级,则可以手动设置一些自定义 LayoutParams:View customView = inflater.inflate(R.layout.custom_action_bar_tabs, null);
customView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
关于android - FrameLayout.onMeasure() 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25318191/