我已经将fragments
类定义为
import android.support.v4.app.Fragment;
public class UserAccountDetailsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.user_account_details_fragment, container, false);
}}
我将自己的活动定义为
import android.support.v4.app.FragmentActivity;
public class HomeScreenActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
UserAccountDetailsFragment userAccDetailsFrag = new UserAccountDetailsFragment();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragments, userAccDetailsFrag );
fragmentTransaction.commit();
}
问题是我无法添加片段。它说UserAccountDetailsFragment应该做成一个片段。但是该类已经扩展了片段。我在这里想念什么?
XML--
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".HomeScreenActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/fragments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
最佳答案
您正在使用support.v4软件包。
您应该尝试以下方法:
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
关于android - 向 Activity 类添加 fragment 时,Android发生冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23755553/