tags:

views:

53

answers:

2

I'm new to Java and Android. I have a piece of code that is used for multiple activities so I moved it into its own library .java file. However, now my findViewById return null where they used to return the right stuff when they were part of the main Activity file with onCreate() and setContentView() calls. How do I make it work inside my library?

Call from the Activity class:

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

The code in the libruary:

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();
    }



}

}

A: 

What you're trying to do seems 'icky' but here's how it would be done.

You'll need to pass the View from the Activity...findViewbyId() is its Member function.

If you provide some sample code, i could be more helpfull.

Edit: Confused Context with View. Oops!

st0le
Why "icky"? Since when placing common code in a separate libruary is "icky"??
@user,For one, you're apparently creating an entire new activity to host your Helper functions, Does Helper Need to extend `ListActivity`?
st0le
@user, `getListView()` returns a Listview...You are trying to find `gray_out_film` within a listview, hence its returning NULL. You'll need the `View` of the LinearLayout of the First Activity. (which im guessing is a ListActivity)
st0le
OK, so how do you do that?
`View v = this.findViewById(R.layout.activity);` Should work, or try `getRootView()`...Sorry, i can't be of much help, i don't have access to my development machine as of now.
st0le
That doesn't work. But, c'mon - hasn't anybody ever needed to move some common code into a separate class and deal with the UI components?
of course, it's been done, i've done the same...I'll get a working code ASAP.
st0le
OK. Let me know when you do. This is a huge bottleneck for me and somehow I don't see any other postings anywhere online about something like this.