【问题标题】:Android getSupportFragmentManager with FragmentActivityAndroid getSupportFragmentManager 与 FragmentActivity
【发布时间】:2017-03-24 00:19:00
【问题描述】:

我正在处理这个活动:

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”,一切正常。

【问题讨论】:

  • 如果我在哪里这样做,我会遇到下一行 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); 的问题mapFragment.getMapAsync(this); Iwnat 使用谷歌地图,这就是为什么我需要类来扩展 FragmentActivity
  • 进行了更改,ViatgeDetallViewFragment 扩展了片段,并且布局有一个 supportMapfragment 代替。似乎不起作用。当我尝试获取“SupportMapFragment”时,与以前相同的问题 + ViatgeDetallViewFragment 中出现新错误
  • 这似乎行得通!确实,更改导入解决了错误!

标签: android android-activity fragment


【解决方案1】:

您应该扩展 Fragment 类而不是 FragmentActivity

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多