0

Basically I am calling a function of fragment from Main Activity. And the function goal is to send api to server get data back from parser class and set it to Fragment RecyclerView. But its giving me an error and pointing in parser class on progressDialog = new ProgressDialog(this.context); Error is below.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference

Here is my code in main activity which is calling my function from fragment.

 if(adapterPosition==0) {
     SecondFragment secondFragment=new SecondFragment();
     secondFragment.SearchCategoresFunction(text);
  }

Here is my function in Fragment:

 public void SearchCategoresFunction(final String searchTxt){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mparser = new SecondParseDataClass(getContext());
                mparser.setOnDataRetrievalCallback(new OnDataRetrievalCallback() {
                    @Override
                    public void onDataRetrieval(ArrayList<DataStored> dataSet) {
                        dataStoredArrayList.addAll(dataSet);
                        myRecyclerAdapter.notifyDataSetChanged();
                    }
                });
                mparser.execute("http://192.168.3.10/fetchtext.php", "2",searchTxt);

            }
        }, 0);
    }

Please guide me how can I remove this error.

Majid Ali
  • 85
  • 1
  • 9

1 Answers1

0

Fragment is null so only the error will popup, so before call the function you set one Id to fragment, then you check the fragment is null or not. Follow the below method.

add fragment on following method to your activity..

FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag("Second_Fragment");

if (fragment == null) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Bundle bundle = new Bundle();
    fragmentTransaction.add(R.id.layout_container, SecondFragment.newInstance(bundle), "Second_Fragment").commit();
}

after you check the following case and call that method

SecondFragment secondFragment = (SecondFragment) getSupportFragmentManager().findFragmentByTag("Second_Fragment");
 if (secondFragment!=null) {
    secondFragment. SearchCategoresFunction("String");
}

i hope you this is helpfull for you.. thank you

Mathan Chinna
  • 587
  • 6
  • 22
  • I am new to android please could you tell me which code I have to write in fragment and which I have to write in Main activity ? – Majid Ali Apr 19 '18 at 14:40
  • I have five different Fragment and how can I set tag to every fragment and how get back tag from fragment and then call to fragment function – Majid Ali Apr 19 '18 at 14:42
  • you have 5 fragment right, so you put five different tags from here `Fragment fragment = fragmentManager.findFragmentByTag("Second_Fragment");` and `SecondFragment secondFragment = (SecondFragment) getSupportFragmentManager().findFragmentByTag("Second_Fragment");` – Mathan Chinna Apr 19 '18 at 14:52