如何将getContext传递给Java类不起作用?

dem*_*emo 2 java android progress-bar

我正在创建常见的java类,ProgressBar我得到如下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void app.bridgecenterandstaffmanagament.com.bridgecenterandstaffmanagament.CommonClass.Singletonclass_obj.showProgressBar(android.content.Context)' on a null object reference
                                                                                                                           at app.bridgecenterandstaffmanagament.com.bridgecenterandstaffmanagament.Fragments.NotificationFragment.onCreateView(NotificationFragment.java:41)
Run Code Online (Sandbox Code Playgroud)

分段

public class NotificationFragment extends Fragment {


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

    RecyclerView recyclerView;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v = inflater.inflate(R.layout.fragment_notification, container, false);


        recyclerView = v.findViewById(R.id.recylierview_notification);



       // Singletonclass_obj.getInstance().showProgressBar(getActivity());
        Singletonclass_obj.getInstance().showProgressBar(getContext());
       // print();

        return v;
    }


}
Run Code Online (Sandbox Code Playgroud)

Java

public class Singletonclass_obj extends Application{
    private  ProgressBar progressBar;
    private static Singletonclass_obj singletonclassobj;



    public static Singletonclass_obj getInstance() {
        return singletonclassobj;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        singletonclassobj = this;
    }

    public void showProgressBar(Context context)
    {
      //  Toast.makeText(context, "welcometoindia", Toast.LENGTH_SHORT).show();
        progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
    }

    public void hideProgressBar()
    {
        if (progressBar !=null && progressBar.isShown())
            progressBar.setVisibility(View.INVISIBLE);
     }

}
Run Code Online (Sandbox Code Playgroud)

相同的代码工作正常我的上一个Project..please帮助我任何一个.

Nil*_*hod 5

按我的上述评论的问题

试试这个添加你Singletonclass_obj的清单文件里面application tag like below code

<application
        android:name=".Singletonclass_obj"<!--add here your Singletonclass_obj class -->
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)