Call method in Fragment from some class

Published

i’ve been working on Android App in AndroidStudio.
Application has Fragment, let’s call it MainFragment, and seperate Java Class file, let’s call it SQLiteControler.class
The problem is when i try to call method from Fragment in SQLiteController im getting null object reference as error
Here is some minified code:

MainFragment

public class MainFragment extends Fragment {
            ...
            //TextViews & ETC
            public View onCreateView(@NonNull LayoutInflater inflater,
                                     ViewGroup container, Bundle savedInstanceState) {
                view = inflater.inflate(R.layout.fragment_profil, container, false); 
           return view:
    }
    public void someMethod(...){
     textView.setText("abc");
    //somecode
   }
}

SQLiteController

public class SQLiteDataHelper {
    private Context context;
    MainFragment mainFragment;

    public SQLiteDataHelper(Context context){
        this.context = context;
        mainFragment = new MainFragment();

    }
    public void anothermethod(){
    mainFragment.someMethod(...)
    }
}

Note: This is minified code

I have tried, a few my solutions like geting instance from MainFragment & ETC, and many more possible solutions from StackOverflow, but still getting same null object reference.

Thanks on help, i appriciate it, happy coding 🙂

Source: Android Questions

Published
Categorized as android, class, fragment, java, methods

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq