0

I checked several solutions on StackOverflow but nothing fitted my special problem.
I have several Fragments and I want a google map activity in one of those.
So I prepared the layout and created a new google map Activity with the necessary name.

When I try to run it it says:

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

Furthermore it is written

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

After doing this the message did not disappear.

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));
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
egjada
  • 119
  • 2
  • 12

3 Answers3

0

FragmentScreenD which is extended From FragmentActivity does not Fragment and the second arguement of FragmentManager's replace method needs a Fragment , FragmentScreenD can be started via intent and its behavior is like an Activity, change FragmentActivity to Fragment and implement the methods in FragmentScreenA,B,C,...

Milad Ge
  • 160
  • 1
  • 9
0

I have several fragments and I want a google map activity in one of those.

You don't want an Activity, you want a Fragment, as the error is trying to tell you.

You can add a new SupportMapFragment() directly to the FrameLayout, or you need to instead extend that rather than FragmentActivity

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Just take a map fragment in fragment XML and check this link it will helpful to solve problem

android MapView in Fragment