如何修复 fragmentTransaction.replace 错误无法转换为 Fragment
How to fix fragmentTransaction.replace error and cannot be converted to Fragment
我正在学习 Android 几个星期。
我现在正在尝试使用片段。并使用来自 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>
检查你的进口。您可能使用了错误的包。
有两个片段类:
以及两个FragmentTransaction 类:
您不能将 v4 FragmentTransaction 与本机片段或相反的片段一起使用。
您可能还想阅读 Support Library Features 文档。
我正在学习 Android 几个星期。
我现在正在尝试使用片段。并使用来自 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>
检查你的进口。您可能使用了错误的包。
有两个片段类:
以及两个FragmentTransaction 类:
您不能将 v4 FragmentTransaction 与本机片段或相反的片段一起使用。
您可能还想阅读 Support Library Features 文档。