向活动类添加片段时发生Android冲突

时间:2014-05-20 09:11:22

标签: android android-fragments

我已将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"
    />

5 个答案:

答案 0 :(得分:1)

您正在使用support.v4包。
你应该尝试以下方法:

FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();

答案 1 :(得分:1)

你根本无法将常规SDK中的Fragment API与android-support SDK混合使用。

您必须拨打getSupportFragmentManager()而不是getFragmentManager()

getSupportFragmentManager()返回android.support.v4.app.FragmentManager。

我还建议首先检查你的片段是否已经添加,如果你的片段没有添加到后面的堆栈,请在片段上使用setRetainInstance(true)。

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class HomeScreenActivity extends FragmentActivity {

    private static final String FRAGMENT_TAG = "custom_tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    Fragment frag = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
    if (frag == null) {
        UserAccountDetailsFragment userAccDetailsFrag = new   UserAccountDetailsFragment();
        userAccDetailsFrag.setRetainInstance(true);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragments, userAccDetailsFrag, FRAGMENT_TAG);
        fragmentTransaction.commit();
    } 

}

答案 2 :(得分:0)

这是问题所在:

android.app.FragmentManager fragmentManager = getFragmentManager();       android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

替换为:

android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();       android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

并在xml中使用它:

<fragment
            android:id="@+id/map_relative_fragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

并在清单中提及:

 <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

和地图键

答案 3 :(得分:0)

试试这个,

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.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
      android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(R.id.fragments, userAccDetailsFrag );
      fragmentTransaction.commit();
    }

这可能会对你有所帮助

答案 4 :(得分:0)

      UserAccountDetailsFragment userAccDetailsFrag = new   UserAccountDetailsFragment();
                 FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .add(R.id.fragments, userAccDetailsFrag).commit();

试试这个