在另一个未扩展FragmentActivty的活动中实例化Map Fragment

时间:2017-09-11 08:09:05

标签: java android google-maps android-fragments

我有一个名为 MarkAttendance 的活动,它有一个片段和两个按钮,我创建了一个单独的 MapActivity 类,扩展了 FragmentActivty 。现在我想将MapActivity实例化为MarkAttendance。

MarkAttendance

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

    punchIn = (Button) findViewById(R.id.punchIn);
    punchOut = (Button) findViewById(R.id.punchOut);

    tv = (TextView) findViewById(R.id.tv);

    SupportMapFragment supportMapFragment = (SupportMapFragment) 
    getSupportFragmentManager().findFragmentById(R.id.map1);
    FragmentTransaction fragmentTransaction 
    =getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.map1, supportMapFragment,"Fragment");
    fragmentTransaction.commit();
 }

MapActivty

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mark_attendance);

    SupportMapFragment mapFragment = (SupportMapFragment) 
getSupportFragmentManager()
            .findFragmentById(R.id.map1);
    mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in 
Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
 }
}

MarkAttendance.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/FrameLayout"
android:background="@color/Cyan">

<android.support.v7.widget.AppCompatButton
    android:id="@+id/punchOut"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="50dp"
    android:layout_marginStart="90dp"
    android:layout_marginEnd="90dp"
    android:text="@string/PunchOut"
    android:textColor="@color/White"
    android:drawableStart="@drawable/ic_punch"
    android:background="@drawable/punchout_button"
    style="@style/Base.Widget.AppCompat.Button.Borderless"/>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map1"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="250dp"
 tools:context="com.example.administrator.employeeattendance.MapsActivity"/>

</FrameLayout>

MapActivity 的ID应该指向 markAttendance xml 吗? What I am trying to create is something similar to this.

0 个答案:

没有答案