0

I got an custom class. Which works great.

public class FocusGameView extends SurfaceView implements Runnable

At the activity itself I want to put the 'FocusGameView' on a view that I already created on the xml file.

so I tried to use the 'inflate' like this:

public class FocusGame extends Activity {

    FocusGameView fgv;
    View v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fgv= new FocusGameView(this);

        v=(View) findViewById(R.id.frame_focus_game);

        LayoutInflater mInflater;
        mInflater = LayoutInflater.from(fgv.getContext());

        v = mInflater.inflate(R.layout.activity_focus_game, null);


        setContentView(R.layout.activity_focus_game);
}

The result of this code is opening the activity and set the layout. without put the custom view on the view itself.

I really hope you could help me with that.

Thanks in advance;

Yaniv.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
YanivGK
  • 893
  • 12
  • 18

1 Answers1

1

You can only add a view to a view group, not a view, so lets image your View v is a RelativeLayout:

public class FocusGame extends Activity {

FocusGameView fgv;
RelativeLayout v;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_focus_game);

    v=(View)findViewById(R.id.frame_focus_game);

    fgv= new FocusGameView(this);
    v.addView(fgv); //You only need to add the view to a parent to make it appear
}
nunoh123
  • 1,087
  • 1
  • 10
  • 17
  • Unfortunately, it does not work. By debug testing, I can see that there is a problem line: v.addView(); It's write that there is a fatal exception. – YanivGK Feb 24 '14 at 15:08
  • It all depends on what is the kind of view of R.id.frame_focus_game – nunoh123 Feb 24 '14 at 21:51
  • Sorry my bad, i tested the code and this line setContentView(R.layout.activity_focus_game); needs just after the super.contructor so that the compiler knows where to find the views we are searching for. – nunoh123 Feb 25 '14 at 10:36
  • It doesn't cast it. What is the "R.id.frame_focus_game" id's of? A RelativeLayout or a View? – YanivGK Feb 26 '14 at 15:38
  • It's the RelativeLayout itself! Working great. Ty :) – YanivGK Feb 26 '14 at 16:12