Domanda

i'm programming an android game and therefore im using a class that extends surfaceview.

My Activity looks like this:

public class GameActivity extends FragmentActivity {

private Fragment fragEnd;

private GameView gameView;


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

    gameView = new GameView(this);

    setContentView(gameView);


    fragEnd = (FragmentEnd) Fragment.instantiate(this, FragmentEnd.class.getName(), null);

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    ft.add( WHAR COMES IN HERE??? , fragEnd);
    ft.commit();

}
...

The GameView class extends the surfaceview. My activity_game.xml layout file looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GameActivity" 
    android:id="@+id/myView">

</RelativeLayout>

So there is nothing special defined in my xml. Now i want to use fragments in the game for options etc. I read some tutorials and added the components to my (Fragment)Activity. But i recieve an IllegalArgumentException: "no view found" while trying to create the fragment. i know that the problem is, that the current view (Surfaceview) cannot be found. in the tutorials, they only worked with layout xmls like writing:

setContentView(R.layout.activity_main);

And then get the component (for example the Relativelayout id "relativeLayoutView") of the activity_main to define the

ft.add( R.id.relativeLayoutView , fragEnd);

But in my case there is nothing in my activity_main because all happens ins the gameView-class. How do i solve this to display fragments?

È stato utile?

Soluzione

Create a layout like this and set to the activity .. Also add an interface in GameView and implement the interface in activity. when you show some thing in fragment , call the interface method from GameView and add the fragment into the activity using R.id.container or other approach is to show the options as dialog.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.examples.workapp.app.GameView
    android:id="@+id/gameView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"/>
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    </FrameLayout>
 </LinearLayout>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top