带有FragmentActivity的Android getSupportFragmentManager

时间:2017-03-24 00:19:01

标签: android android-activity fragment

我正在使用此活动:

public class ViatgeDetallViewActivity extends AppCompatActivity {

    ViatgeDetallViewFragment fragment;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);


            fragment = new ViatgeDetallViewFragment();        
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.activity_fragment_view, fragment)  
                    .commit();


        }
    .....
    }

类ViatgeDetallViewFragment是:

public class ViatgeDetallViewFragment extends FragmentActivity implements OnMapReadyCallback {

    public ViatgeDetallViewFragment() {
    }


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

    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view, container, false);


        SupportMapFragment mapFragment =
                (SupportMapFragment)
                        getSupportFragmentManager()
                                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


        return rootView;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        ...

}

活动的简单布局为:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/tlbMenuView"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        />

    <FrameLayout
        android:id="@+id/activity_fragment_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

fragment_view是:

<LinearLayout android:id="@+id/layout_fragment_view" xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View"
        />       
    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_view_map"
        class="com.google.android.gms.maps.MapFragment"></fragment>

</LinearLayout>

当我尝试添加片段时,问题出现在“ViatgeDetallViewActivity”中:

 fragment = new ViatgeDetallViewFragment();        
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.activity_fragment_view, fragment)  
                    .commit();

我收到以下错误:

Error:(68, 21) error: no suitable method found for add(int,ViatgeDetallViewFragment)
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; ViatgeDetallViewFragment cannot be converted to Fragment)

我尝试过相同的代码,但片段类扩展了“fragment”而不是“FragmentActivity”,一切都很顺利。

1 个答案:

答案 0 :(得分:0)

您应该扩展 Fragment 类,而不是 FragmentActivity