RealNames
RealNames

Reputation: 3

Java class extends MainActivity but findViewById comes back null?

This class extends my main Activity.

public class Numbers extends MainActivity{

public ArrayList<ImageView> getNumbers () {
    ArrayList<ImageView> numbers = new ArrayList<>();

    ImageView one = (ImageView) findViewById(R.id.one);
    numbers.add(one);
    return numbers;
}

And I've done some digging but can figure out why my variable "one" is coming back null.
My MainActivity has a ContentView set.

This is the content of my onCreate in MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView start = (ImageView) findViewById(R.id.start);
    sceneRoot = (ViewGroup) findViewById(R.id.scene_root);
    questionView = findViewById(R.id.questionView);
    startView = findViewById(R.id.startView);
    gameOverView = findViewById(R.id.gameOver);

    animSlide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide);
    animSlide.setAnimationListener(this);
    animZoom = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_fade);
    animZoom.setAnimationListener(this);

    set.addTransition(new Fade())
       .addTransition(new Slide(Gravity.RIGHT));

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getQuestion();
            TransitionManager.beginDelayedTransition(sceneRoot, set);
            startView.setVisibility(View.GONE);
            questionView.setVisibility(View.VISIBLE);
        }
    });
}

public void getQuestion (){
    time = (TextView) findViewById(R.id.timeBar);
    time.startAnimation(animSlide);
}

I don't call getNumbers() until after start has been clicked and the animation has started.

public void onAnimationStart(Animation animation){
    if(animation == animSlide) {
        final Questions questions = new Questions();

        Numbers n = new Numbers();

        for (int i = 0; i < n.getNumbers().size(); i++) {
            n.getNumbers().get(i).setVisibility(View.GONE);
            n.getNumbersTen().get(i).setVisibility(View.GONE);
        }
        n.getNumbers().get(0).setVisibility(View.VISIBLE);

EDIT:

If anyone was wondering, I got it to work by extending the class as a Fragment instead of my MainActivity. Then I just used the fragment in my xml.

Upvotes: 0

Views: 343

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191701

Because you extended an Activity class doesn't mean setContentView gets called for that class also. It will only do so if properly started and you call super.onCreate(bundle) from your own implementation of onCreate within Numbers

Basically, you should never new any Activity. It has no life-cycle, and therefore no content view, so findViewById just won't work.

Numbers n = new Numbers();

You could not extend anything and have a data-only class around your list of images.

public class Numbers {
    private List<ImageView> numbers = new ArrayList<ImageView>();
    public Numbers() {}
    public void addNumber(ImageView v) { numbers.add(v); }
    public List<ImageView> getNumbers() { return numbers; }
}

And from MainActivity you can find and add as you want.

Number n = new Numbers();
n.addNumber((ImageView) findViewById(R.id.one));

However, I don't know if that is useful, really...

Maybe a Fragment would serve a better purpose if you want a "sub-view" of your Activity, but it's hard to tell.

Upvotes: 1

Related Questions