2016-10-02 59 views
0

我正在学习几个星期的Android。如何修复fragmentTransaction.replace错误,无法转换为片段

我现在试图使用片段。并使用来自https://github.com/dogriffiths/HeadFirstAndroid 想法来自书本 https://books.google.com/books?id=qkzrCQAAQBAJ&pg=PA275&lpg=PA275&dq=pure+java+class+fragments&source=bl&ots=Ayz-JbqS4r&sig=f2DBRNgX_wrvBnGrDhJiIfHNhrE&hl=en&sa=X&ved=0ahUKEwjD6OHS67rPAhUG1CYKHczIBnAQ6AEIMDAD#v=onepage&q=pure%20java%20class%20fragments&f=false

我越来越

'replace(int, android.app.fragment)' in 'android.app.fragmentTransaction' cannot be applied to

这也

Error: ContactDetailFragment cannot be converted to Fragment

源代码

import android.app.Activity; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.View; 


    public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{ 



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


     } 

     @Override 
     public void itemClicked(long id){ 
      //method also defined in the listener 
      View fragmentContainer = findViewById(R.id.fragment_detail_container); 
      if (fragmentContainer != null){ 
       ContactDetailsFragment detailsFragment = new ContactDetailsFragment(); 
       FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
       detailsFragment.setContact(id); 
       //fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment); 
       fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
       fragmentTransaction.commit(); 
      } 

     } 

    } 

contactactivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_contact" 
    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" 
    android:baselineAligned="false" 
    tools:context="esu.uco.rawal.p5rabina.ContactActivity"> 
     <fragment 
      class="esu.uco.rawal.p5rabina.ContactListFragment" 
      android:layout_width="0dp" 
      android:layout_weight ="1" 
      android:layout_height="match_parent" 
      android:id="@+id/contact_list_frag"/> 


     <FrameLayout 
      android:id="@+id/fragment_detail_container" 
      android:layout_width="0dp" 
      android:layout_weight="2" 
      android:layout_height="match_parent" 
      > 

     </FrameLayout> 

</LinearLayout> 
+0

R.id.fragment_detail_container它是容器还是视图?你可以在你想添加这个片段的地方添加xml文件布局吗? – EEJ

+0

@EEJ它的容器。我已经添加了xml布局。请检查一下。 – robinleathal

+0

听起来像你已经导入了错误的片段类,或者应该使用supportFragmentManager而不是 –

回答

相关问题