2017-01-05 2 views
0

Я пытаюсь создать приложение для викторины с боковой панелью, состоящей из флагов (кнопок изображений), и когда вы нажимаете на каждый флаг, вы можете ввести страну, в которой она принадлежит, в фрагменте справа с правой стороны экрана. Однако я получаю сообщение об ошибке внутри оператора if в файле Play.java, где он говорит:Фрагменты студии Android

fragment = new FragmentOne();

и

фрагмент = новый 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); 
} 



} 

ли кто-нибудь знаете, что я делаю неправильно?

+1

какая ошибка вы получаете? – Hoo

+0

Вам нужно добавить пустой конструктор в свой класс фрагментов. –

+0

Ошибка, которую я получаю: «Несовместимые типы: обязательно: android.app.Fragment, found: layout.FragmentOne» –

ответ

2

В вашем классе Play.java ваша переменная Fragment имеет тип android.app.Fragment. Хотя ваш FragmentOne.java (и я предполагаю, что ваш FragmentTwo.java также) класс имеет тип android.support.v4.app.Fragment.

Итак, в классе Play.java вы пытаетесь инициализировать переменную Fragment с помощью wrong type.

Либо:

  • Сделать свой FragmentOne.java и FragmentTwo.java расширяет android.app.Fragment вместо версии поддержки один
  • сделать вашу Fragment переменную в Play.java типа android.support.v4.Fragment, изменив импорт.
Смежные вопросы