• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to properly change between a surfaceview and activity?

Im getting all sorts of errors when i try to run an intent inside my gameview. What im trying to do is basicly to go back to my menu activity after my game is over in my gameview. I cant seem to find any projects or samples that does this properly. Im hoping to use fragments aswell at some point for the transition, its not possible at the games current state however.
 
Im changed my application and are now using fragments, which are working very well for the most parts. However as part of my application i use a costum gameview to run the gameplay part of my application, and after end game, i cant figure out how to start the endscreen_fragment. For some reason when i run
getFragmentManager().beginTransaction().replace(R.id.fragment_container, new Fragment_Menu_Main()).commit();
which i use to swap between all other fragments, the fragment manager returns null. I belive the fragmentmanager is set to null because i leave my fragmentcycle to use the gameveiw instead, and my question is now, how do i stop it from doing that?
Or how do i create a new fragment manger so i can continue from gameview to endscreen_fragment?

Here is my Fragment_Game class that start my gameview class, and after end game gets called its GameOver() method, which works fine, besides the null fragmentmanager:

Code:
public class Fragment_Game extends Fragment {

    private GameView gameView;

    private static Fragment_Game instance;


    public static Fragment_Game getInstance()
    {
        if(instance == null)
        {
            instance = new Fragment_Game();
        }
        return instance;
    }


    public Fragment_Game()
    {
        // Required empty public constructor
    }

    [USER=1021285]@override[/USER]
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_game, container, false);

        gameView = (GameView) view.findViewById(R.id.ourGameview);

        return  view;
    }

    public void GameOver()
    {
// ERROR HERE, fragmentmanger suddenly null
        getFragmentManager().beginTransaction().replace(R.id.fragment_container, new Fragment_Endscreen()).commit();
    }
 
Last edited by a moderator:
What is null here? That line is a composite statement. Can you include the stack trace please?
It looks like its the fragmentManager that are null?, however you can see from the attached image that my gameview class is supposed to be null aswell, but i doubt thats it as the code should have left the gameview behind at this point and shouldnt need it to swap to endscreen_fragment.

Logcat:
05-22 13:11:11.732 7730-7767/com.example.pc.fragmentbase W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
05-22 13:11:11.732 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.Fragments.Fragment_Game.GameOver(Fragment_Game.java:57)
05-22 13:11:11.732 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.MapObjects.Goal.collect(Goal.java:62)
05-22 13:11:11.734 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.MapObjects.Goal.canCollect(Goal.java:48)
05-22 13:11:11.734 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.Other.Player.doCollision(Player.java:289)
123.png

05-22 13:11:11.734 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.Other.GameObject.onCollisionStay(GameObject.java:138)
05-22 13:11:11.735 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.Other.GameView.update(GameView.java:184)
05-22 13:11:11.735 7730-7767/com.example.pc.fragmentbase W/System.err: at com.example.pc.fragmentbase.Other.GameThread.run(GameThread.java:53)
 
I'm slightly confused about your code because normally Fragments are managed by the parent Activity, not the individual Fragments themselves.
If you wish to swap out one Fragment for another then this should really be handled by the Activity.
 
Ive restructured my app and are now as you recommended trying to use the Activity to handle the switching fragments, however it stil wont allow me to call any method that change fragments, after ive left a fragment class to use my gameview class. When my gameview is done and i try to call my activitys method with this code:

Activity:
Code:
public void changeFragment(String _newFragment)
{
    switch (_newFragment)
    {
        case "Game":
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new Fragment_Game()).commit();
            break;

        case "EndScreen":
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new Fragment_Endscreen()).commit();
            break;
    }
}

Fragment_Game:
My gameview calls this fragments GameOver method at end game, this happens just fine:

Code:
 public void GameOver()
    {
/*        getFragmentManager().beginTransaction().replace(R.id.fragment_container, new Fragment_Endscreen()).commit();*/
        ((MainActivity)getActivity()).changeFragment("EndScreen");
    }

However i get an error that says : Inconvertible types; cannot cast 'com.example.pc.fragmentbase.Other.Player' to 'com.example.pc.fragmentbase.Fragments.Fragment_Game'

Is this an error you have seen in this context before? Ive familiar with the error but in regards to fragments im not sure what has happend.
 
Last edited by a moderator:
Im not really getting much information i can post from the logcat or debugger, just this. Also now im getting the same casting error, just on a button object, which makes absolutely no sense to me as it wants to be cast into a fragment_game for some reason. I wonder if you could be interested in fixing the error for me for a price? i bet its pretty simple if your experienced with fragments, and im starting to panic here, i need to have the project done tomorrow afternoon, and im getting nowhere with this last problem :/

312.png
 
That error does not relate to the line of code you're highlighting.

I suspect that the line

Code:
((Fragment_Game)this).gameView = ...

is located in your PowerUpButton class, which would cause your incompatible type conversion error.
 
Well I'm fairly stumped here, because somewhere in your code you have a ClasscastException.
This would become apparent as a runtime error in your Logcat.
 
Back
Top Bottom