【问题标题】:Map in Fragment: incompatible types cannot be converted to FragmentFragment 中的映射:不兼容的类型无法转换为 Fragment
【发布时间】:2017-12-28 19:29:58
【问题描述】:

我在 StackOverflow 上检查了几个解决方案,但没有一个适合我的特殊问题。
我有几个片段,我想在其中一个片段中进行谷歌地图活动。
所以我准备了布局并创建了一个具有必要名称的新谷歌地图活动。

当我尝试运行它时,它会说:

Error:(20, 42) error: incompatible types: FragmentScreenD cannot be converted to Fragment

另外写了

"Wrong 2nd argument type. Found "com.example.name.yapplication.FragmentScreenD", required "android.support.v4.app.Fragment".

执行此操作后,消息并没有消失。

package com.example.name.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;


public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment1, new FragmentScreenA()).commit();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment2, new FragmentScreenB()).commit();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment3, new FragmentScreenC()).commit();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment4, new FragmentScreenD()).commit();
    }
} 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:weightSum="2" >

    <FrameLayout
        android:id="@+id/fragment3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="0dp"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/fragment4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1" />
</LinearLayout>

package com.example.name.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class FragmentScreenD extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_screen_d);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

【问题讨论】:

  • 你是否从 android.SUPPORT 包中导入了所有内容?

标签: android android-fragments


【解决方案1】:

我有几个片段,我想要其中一个片段中的谷歌地图活动。

你不想要一个 Activity,你想要一个 Fragment,因为错误试图告诉你。

您可以将new SupportMapFragment() 直接添加到 FrameLayout,或者您需要扩展它而不是 FragmentActivity

【讨论】:

    【解决方案2】:

    从FragmentActivity扩展而来的FragmentScreenD没有Fragment,FragmentManager的replace方法的第二个参数需要一个Fragment,FragmentScreenD可以通过intent启动,其行为类似于Activity,将FragmentActivity改为Fragment,实现FragmentScreenA,B中的方法,C,...

    【讨论】:

      【解决方案3】:

      只需在片段 XML 中获取地图片段并检查此链接将有助于解决问题

      android MapView in Fragment

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-26
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        相关资源
        最近更新 更多