2017-01-05 164 views
0

我正在嘗試使用由標誌(圖像按鈕)組成的側欄創建測驗應用程序,並且當您單擊每個標誌時,可以在右側的片段中鍵入所屬的國家/地區屏幕的手邊。然而,我在我的Play.java文件中的if語句中出現錯誤,它說:Android Studio碎片

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); 
} 



} 

有誰知道我在做什麼錯了?

+1

你得到什麼錯誤? – Hoo

+0

您需要在片段類中添加一個空構造函數。 –

+0

我得到的錯誤是:「不兼容的類型:required:android.app.Fragment,found:layout.FragmentOne」 –

回答

2

在您的Play.java類中,您的Fragment變量類型爲android.app.Fragment。雖然你的FragmentOne.java(和我假設你的FragmentTwo.java)類是android.support.v4.app.Fragment類型。

因此,在您的Play.java類中,您嘗試使用wrong type初始化Fragment變量。

或者:

  • 讓您FragmentOne.javaFragmentTwo.java延伸android.app.Fragment而不是支持版本一個
  • 通過改變你的導入使在android.support.v4.Fragment型的Play.javaFragment變量。