片段中的地图:不兼容的类型无法转换为片段

时间:2017-07-22 22:26:44

标签: android android-fragments

我在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));
    }
}

3 个答案:

答案 0 :(得分:0)

从FragmentActivity扩展的FragmentScreenD不会碎片,FragmentManager的替换方法的第二个争论需要一个Fragment,FragmentScreenD可以通过intent启动,它的行为就像一个Activity,将FragmentActivity更改为Fragment并实现FragmentScreenA中的方法,B ,C,......

答案 1 :(得分:0)

  

我有几个片段,我想在其中一个片段中进行谷歌地图活动。

你不想要一个活动,你想要一个片段,因为错误试图告诉你。

您可以直接向FrameLayout添加import sys from PyQt5 import QtWidgets if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) tablewidget = QtWidgets.QTableWidget(4, 4) widget = QtWidgets.QWidget() widgetText = QtWidgets.QLabel("<b>foo bar</b>") widgetLayout = QtWidgets.QHBoxLayout() widgetLayout.addWidget(widgetText) widgetLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize) widget.setLayout(widgetLayout) tablewidget.setCellWidget(0, 0, widget) tablewidget.resizeColumnsToContents() tablewidget.show() sys.exit(app.exec_()) ,或者您需要扩展它而不是片段活动

答案 2 :(得分:0)

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

<强> android MapView in Fragment