java.lang.NullPointerException: Android Attempt to invoke virtual method error

前端 未结 1 851
长发绾君心
长发绾君心 2021-01-26 07:42

My android webview fragment app is getting crashed, after i have added a button to go from one fragment to another, the error on the console is:

E/AndroidRuntime         


        
相关标签:
1条回答
  • 2021-01-26 08:12

    Try below:

    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
        mActivity = activity;
    }
    

    and then to prevent leaks:

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }
    

    Now to use MainActivity do:

    ((MainActivity)mContext).setUrl(url);
    
    0 讨论(0)
提交回复
热议问题