FindViewById returns null when moving to the library file

I am new to Java and Android. I have a piece of code that is used for several actions, so I moved it to my own .java library file. However, now my findViewById returns null, where they used to return the material when they were part of the main Activity file with onCreate () and setContentView () calls. How to make it work inside my library?

A call from the Activity class:

helper.popupControl (getListView (), getBaseContext (), "on");

Code in libral:

public class Helper extends ListActivity { public void popupControl (View v, Context context, String on_off) { Animation aFilm = AnimationUtils.loadAnimation(context, R.anim.fade_in); aFilm.reset(); View vFilm = (View) v.findViewById(R.id.gray_out_film); if(vFilm==null) { Toast maxToast = Toast.makeText(context, "View is null! "+R.id.gray_out_film+", View:"+v.toString(), Toast.LENGTH_LONG); maxToast.setGravity(Gravity.CENTER, 0, 0); maxToast.show(); } else { Toast maxToast = Toast.makeText(context, "View is not null!", Toast.LENGTH_SHORT); maxToast.setGravity(Gravity.CENTER, 0, 0); maxToast.show(); } } } 
+1
source share
3 answers

Call Activity.setContentView(int resID) before calling findViewbyId()

+1
source

What you are trying to do seems to be icky, but here is how it will be done.

You need to pass the View from the Activity ... findViewbyId () is its member function.

If you provide some sample code, I can be more helpful.

Edit: Confused Context using View . Unfortunately,

0
source

This happened to me, and the solution was to remove main.xml from the new project. Somehow it interfered with main.xml from the library project, although I had in mind the full name (for example: library.package.R.layout.main).

0
source

All Articles