Android开发:调用mAppBarLayout.addOnOffsetChangedListener遇空指针异常
Hey there! Let's figure out why you're hitting that NullPointerException when trying to add the offset listener to your AppBarLayout.
问题根源
The error message makes it clear: you're calling addOnOffsetChangedListener on a null AppBarLayout reference. Looking at your user_profile.java code, I spotted a critical missing step in your onCreate method: you never called setContentView() to inflate your layout file! Without loading the layout first, all your findViewById() calls in bindActivity() will return null—including mAppBarLayout.
On top of that, the activity_user_profile.xml snippet you shared is incomplete (tags like FrameLayout and CoordinatorLayout aren't properly closed), which would also break layout inflation even if you did call setContentView().
解决方案
1. Add setContentView() to your Activity's onCreate method
Update your onCreate method to inflate the layout before binding views. This ensures the system can find your AppBarLayout and other views:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Add this line FIRST to load your layout file setContentView(R.layout.activity_user_profile); bindActivity(); mAppBarLayout.addOnOffsetChangedListener(this); mToolbar.inflateMenu(R.menu.profile_menu); startAlphaAnimation(mTitle, 0, View.INVISIBLE); }
2. Fix your incomplete layout file
Make sure all XML tags are properly closed, and add the missing Toolbar and scrollable content view (required for AppBarLayout scrolling to work). Here's the corrected layout:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".user_profile" tools:ignore="RtlHardcoded"> <android.support.design.widget.AppBarLayout android:id="@+id/profile_appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/profile_collapsing_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"> <ImageView android:id="@+id/profile_cover_img" android:layout_width="match_parent" android:layout_height="300dp" android:scaleType="centerCrop" android:src="@drawable/material_background" android:tint="#11000000" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.9" /> <FrameLayout android:id="@+id/profile_framelayout_title" android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="bottom|center_horizontal" android:background="@color/colorPrimary" android:orientation="vertical"> <!-- Match the LinearLayout and TextView from your Java code --> <LinearLayout android:id="@+id/profile_linearlayout_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/profile_textview_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Profile Title" android:textColor="@android:color/white" android:textSize="24sp" /> </LinearLayout> </FrameLayout> <!-- Add the Toolbar referenced in your Java code --> <android.support.v7.widget.Toolbar android:id="@+id/profile_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <!-- Add a scrollable view to enable AppBar scrolling behavior --> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- Insert your profile content (like user info, buttons, etc.) here --> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
3. Double-check view ID matches
Ensure the IDs in your Java code (R.id.profile_appbar, R.id.profile_toolbar, etc.) exactly match the IDs in your layout file. A tiny typo here would still cause findViewById() to return null.
Why this works
setContentView()tells the Activity to parse your XML layout, making all views available for lookup.- Properly closed XML tags prevent layout inflation errors.
- The
NestedScrollViewwithapp:layout_behaviorenables theAppBarLayoutto respond to scroll events, which is required for your offset listener to trigger.
内容的提问来源于stack exchange,提问作者Farid Chowdhury

