Android :: java.lang.NullPointerException: 尝试在空对象上调用虚方法 'void android.widget.ProgressBar.setVisibility(int)'

Android :: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object

在我的主页片段上,我有一个 webview,在顶部有一个进度条,它将显示 webview 的进度(加载),但出于某种我可以理解的原因 每次我想访问进度条并将可见性设置为某些内容时,我都会收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference

public class HomeFragment extends Fragment {  

    private ProgressBar progressBarHorizontal;

    public HomeFragment() {
        // Required empty public constructor
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1: {
                    webViewGoBack();
                }
                break;
            }
        }
    };

    @Override
    public void onCreate( @Nullable Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

 
    }

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        // Inflate the layout for this fragment
        View view = inflater.inflate( R.layout.fragment_home, container, false ); 
        return view;
    }

    @Override
    public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {
        super.onViewCreated(view, savedInstanceState);


        progressBarHorizontal = view.findViewById(R.id.progress_bar);
       
        progressBarHorizontal.setVisibility(View.INVISIBLE);
    }
   
}

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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" >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/mainViewLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ProgressBar
            android:id="@+id/progress_bar"
            android:progress="100"
            style="@android:style/Widget.Material.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:max="100"
            android:visibility="invisible"
            tools:visibility="visible"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:progressTint="@color/colorAccent"
            app:layout_constraintBottom_toTopOf="@+id/fragment_container"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar"/>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:translationZ="-10dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_menu"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/bottom_navigation"
            android:background="@drawable/gradient_bottom_bar"
            app:tabIndicatorColor="@color/black"
            app:itemIconTint="@color/white"
            app:itemTextColor="@color/white"
            app:labelVisibilityMode="labeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout> 
 

</androidx.constraintlayout.widget.ConstraintLayout>

好吧,我不明白为什么你需要一个片段的构造函数,但无论如何我可以建议另一种方法来膨胀布局,这将导致一个可能用于获取进度条的“绑定”对象就像您检查 findViewById 一样,您会发现它告诉您它可能 return null.

public class HomeFragment extends Fragment {  

    private ProgressBar progressBarHorizontal;
    private FragmentHomeBinding binding; 
    public HomeFragment() {
        // Required empty public constructor
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1: {
                    webViewGoBack();
                }
                break;
            }
        }
    };

    @Override
    public void onCreate( @Nullable Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

 
    }

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
       binding = FragmentHomeBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {
        super.onViewCreated(view, savedInstanceState);


        progressBarHorizontal = binding.progressBar ;
       
        progressBarHorizontal.setVisibility(View.INVISIBLE);
    }
   
}