我試圖片段添加到活動,但是當我beginTransaction
方法片段添加到活動編譯器返回錯誤無法開始Fragement交易
cannot resolve method add
的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;
}
}
main.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>
消息搖籃建立
Error:(77, 21) error: no suitable method found for add(int,PlaceholderFragment) method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment) method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; PlaceholderFragment cannot be converted to Fragment)
使其擴展FragmentActivity。添加不支持活動 –