Home > Mobile >  NullPointerException when invoking AppBarLayout - Android
NullPointerException when invoking AppBarLayout - Android

Time:06-04

I'm trying to make a youtube clone app.

My AppBarLayout from the .XML file is returning :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.StreamingApplicationNactus/com.example.StreamingApplicationNactus.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.appbar.AppBarLayout.setVisibility(int)' on a null object reference

Here is the .XML file:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    tools:context=".Profile_Table_Fragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@ id/newappBar"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#006E7F"
            app:contentScrim="?android:attr/colorControlHighlight"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:id="@ id/newappBar2">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Channel name"
            android:textSize="20dp"
            android:paddingLeft="10dp"
            android:textColor="@color/white"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="6dp"
            android:id="@ id/user_profile_name"/>

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/main_topbar"
            android:theme="@style/Panel"
            />

        </com.google.android.material.appbar.CollapsingToolbarLayout>


        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#006E7F"
            app:tabGravity="center"
            app:tabIndicatorColor="@color/black"
            app:tabIndicatorFullWidth="false"
            app:tabIndicatorHeight="1dp"
            android:id="@ id/tab_Layout"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#FFF"
            app:tabTextColor="@color/white">

            <com.google.android.material.tabs.TabItem
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Home" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Videos" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="About" />


        </com.google.android.material.tabs.TabLayout>


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
        android:id="@ id/pageViewer"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

and in the .java file, I can only write this and I will get the same error:



AppBarLayout appBarLayout;

@Override
    protected void onCreate(Bundle savedInstanceState) {

 appBarLayout =(AppBarLayout) findViewById(R.id.newappBar);
        appBarLayout.setVisibility(View.VISIBLE);
 }

}

The error will be on appBarLayout.setVisibility(View.VISIBLE);

This is my first post here, so please let me know if I need to add more info!

CodePudding user response:

When you are using Fragment, you need to inflate the view.

Remove these codes:

 appBarLayout = (AppBarLayout) findViewById(R.id.newappBar);
 appBarLayout.setVisibility(View.VISIBLE);

And then at your onCreateView. Add these lines:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   final View root = inflater.inflate(R.layout.fragment_table_profile, container, false);
   final AppBarLayout appBarLayout = root.findViewById(R.id.newappBar);
   appBarLayout.setVisibility(View.VISIBLE);
}
  • Related