setContentView of Activity within Activities current View

Discussion in 'Application Development' started by tantonj, Feb 17, 2012.

  1. tantonj

    tantonj New Member
    5

    May 20, 2011
    3
    0
    5
    So I have my main activity. It starts out adding a custom SurfaceView called DrawView as the content view with

    DrawView drawview = new DrawView(parameters);
    this.setContentView(drawview);

    This works great!... now my layout.main.xml is a layout with a video view which I am using to play my cutscenes. I am able to switch to this view by calling a method I made which switches the content view by

    this.setContentView(R.layout.main);

    which works fine when I am calling this method within the Activity. Only problem is I need to call this method from within DrawView! The method I made to switch to my cutscene is public and I do pass the Activity (named Draw) as a parameter of DrawView.

    So when I do call this method from within DrawView my game freezes and will subsequently crash! however it works when method is called locally?

    Why is this happening? anybody able to help? or maybe tried doing this before?
     

    Advertisement

  2. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    as long as you are passing an instance of Context to the method that is starting the activity (and that Context object is not null), then it should work.
    Can we see a copy of the log cat when it freezes, and also some code snippets?
     
  3. alostpacket

    alostpacket Over Macho Grande?
    VIP Member
    513

    Nov 29, 2009
    7,978
    3,603
    513
    Android App Developer
    NY
    Yeah stack trace is crucial here, but I am wondering why you are calling setContentView on the custom SurfaceView (DrawView).


    I dont know the particular problem that is supposed to solve, but the more typical usage of setContentView would just be to layout your activity.

    So, in other words, you would only call setContentView(R.layout.main) once from onCreate() -- then you would put Custom SurfaceView in the XML for R.layout.main or you could add the view programatically via java after the contentView has been set.

    Would something like this not work for what you are doing?
     
  4. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male

    It's perfectly acceptable to use setContentView() with a subclass of SurfaceView. In fact, when working with OpenGL, this is THE way to do it.

    However, I am lost when trying to understand the logic behind the OP's transitions. I simply don't understand what he is trying to accomplish, so I can't offer much help beyond, "Show me the logcat."

    Apologies.
     
    alostpacket likes this.

Share This Page

Loading...