Kara W
Kara W

Reputation: 143

Android Studio fragments

I'm trying to create a quiz app with a sidebar that consists of flags (image buttons), and when you click on each flag, you can type in the country it belongs to in a fragment on the right hand side of the screen. However I'm getting an error inside the if statement in my Play.java file where it says:

fragment = new FragmentOne();

and

fragment = new FragmentTwo();

Play.java

import android.os.Bundle;                                                           
import android.app.FragmentManager;                                                 
import android.view.View;                                                           
import android.view.Menu;                                                           
import android.app.Activity;                                                        
import android.app.Fragment;                                                        
import android.app.FragmentTransaction;                                             

import layout.FragmentOne;                                                          
import layout.FragmentTwo;                                                          


public class Play extends MainActivity {                                            

Fragment fragment;                                                              


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

}                                                                               

public void ChangeFragment(View view) {                                         


    if(view == findViewById(R.id.imageButton10)) {                              
        fragment = new FragmentOne();                                           
        FragmentManager fm = getFragmentManager();                              
        FragmentTransaction ft = fm.beginTransaction();                         
        ft.replace(R.id.fragment_place, fragment);                              
        ft.commit();                                                            
    }                                                                           
    if(view == findViewById(R.id.imageButton9))  {                              
        fragment = new FragmentTwo();                                           
        FragmentManager fm = getFragmentManager();                              
        FragmentTransaction ft = fm.beginTransaction();                         
        ft.replace(R.id.fragment_place, fragment);                              
        ft.commit();                                                            
    }                                                                           
}                                                                               

}                                                                                   

activity_play.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.karolinawullum.quizapp.Play">


    <LinearLayout
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/LinearLayout1">


        <TextView
            android:text="Which country does this flag belong to? Press flag    to answer."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textSize="10sp" />


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/germany"
            android:id="@+id/imageButton10"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="50dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:cropToPadding="true"
            android:onClick="ChangeFragment" />

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/greece"
            android:id="@+id/imageButton9"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:onClick="ChangeFragment"
            android:scaleType="fitXY"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/france"
            android:id="@+id/imageButton8"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/finland"
            android:id="@+id/imageButton7"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/denmark"
            android:id="@+id/imageButton6"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/cyprus"
            android:id="@+id/imageButton5"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/belgium"
            android:id="@+id/imageButton4"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/austria"
            android:id="@+id/imageButton3"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>

    </LinearLayout>

<fragment
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:name="layout.FragmentOne"
    android:id="@+id/fragment_place"
    android:layout_weight="0.72" />


</LinearLayout>

FragmentOne.java

package layout;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentOne extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}



}

Does anyone know what I'm doing wrong?

Upvotes: 0

Views: 162

Answers (1)

Tristan Frascari
Tristan Frascari

Reputation: 110

In your Play.java class, your Fragment variable is of type android.app.Fragment. Though your FragmentOne.java (and I assume your FragmentTwo.java as well) class is of type android.support.v4.app.Fragment.

So in your Play.java class you're trying to initialize the Fragment variable with the wrong type.

Either :

  • Make your FragmentOne.java and FragmentTwo.java extends android.app.Fragment instead of the support version one
  • Make your Fragment variable in Play.java of type android.support.v4.Fragment by changing your import.

Upvotes: 2

Related Questions