【问题标题】:Can not begin Fragement Transaction无法开始分片交易
【发布时间】:2015-09-14 02:00:56
【问题描述】:

我试图将 Fragment 添加到 Activity ,但是当我 beginTransaction 方法将 Fragment 添加到 Activity 编译器返回错误

无法解析方法添加

错误返回的onCreate函数

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.contain, new PlaceholderFragment())
                .commit();
    }
}

片段

 public static  class PlaceholderFragment extends Fragment  {

    public PlaceholderFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.start, container, false);
        rootView.findViewById(R.id.start).setOnClickListener((View.OnClickListener) this);
        return rootView;
    }
}

ma​​in.xml

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     />

start.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">
    <at.markushi.ui.CircleButton
        android:id="@+id/start"
        android:layout_width="163dp"
        android:layout_height="162dp"
        app:cb_pressedRingWidth="8dip"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/image"
        android:layout_alignStart="@+id/image"
        android:src="@drawable/start"
        android:layout_marginBottom="37dp"
        android:layout_alignRight="@+id/image"
        android:layout_alignEnd="@+id/image"
        android:layout_below="@+id/image" />
</LinearLayout>

消息 Gradle 构建

错误:(77, 21) 错误: 没有找到适合 add(int,PlaceholderFragment) 的方法 方法 FragmentTransaction.add(Fragment,String) 不适用 (参数不匹配;int 不能转换为 Fragment) 方法 FragmentTransaction.add(int,Fragment) 不适用 (参数不匹配;PlaceholderFragment 无法转换为 Fragment)

【问题讨论】:

  • 让它扩展 FragmentActivity。活动中不支持添加

标签: android android-fragments fragmenttransaction


【解决方案1】:

记得使用

import android.support.v4.app.Fragment;

而不是

import android.app.Fragment;

这样您的 Fragment 来自支持库。否则编译器无法解析方法add的参数类型,导致错误。

【讨论】:

    【解决方案2】:

    把你的extendsActivity改成FragmentActivity来支持里面的fragment

    【讨论】:

    • 我不能那样工作,在 calss 中使用 beginTransaction 方法扩展 ActionBarActivity 并且工作
    • @error 你检查过导入吗? headuck 在他的回答中说了什么?您的错误说 PlaceFlderFragment 无法转换为片段。这与您的片段类有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多