从导航抽屉运行地图片段

时间:2015-08-07 10:54:01

标签: android

我一直在使用导航抽屉,如果是1,我想在片段中运行谷歌地图API。 但我收到以下错误: ' replace(int,android.app.Fragment)'在' android.app.FragmentTransaction'不能应用于'(int,com.example.matant.test.MapFragment)'

这是主要活动:

    package com.example.matant.test;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ActionBarDrawerToggle actbartoggle;
    private android.support.v7.app.ActionBar actionbar;
    private DrawerLayout drawer;
    private ListView navlist;
    private FragmentTransaction frmt;
    private FragmentManager frm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawer = (DrawerLayout)findViewById(R.id.drawer);
        navlist = (ListView)findViewById(R.id.navlist);
        ArrayList<String> navArr = new ArrayList<String>();
        navArr.add("Home");
        navArr.add("Playing List");
        navArr.add("Manage List");
        navArr.add("Logout");
        navlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,navArr);
        navlist.setAdapter(adapter);
        navlist.setOnItemClickListener(this);
        actbartoggle = new ActionBarDrawerToggle(this,drawer,R.string.opendrawer,R.string.closedrawer);
        drawer.setDrawerListener(actbartoggle);
        actionbar = getSupportActionBar();

        actionbar.setDisplayShowHomeEnabled(true);
        actionbar.setDisplayHomeAsUpEnabled(true);
         frm = getFragmentManager();

        loadDefFrag(0);
    }
    private  void loadDefFrag(int i){
        navlist.setItemChecked(i,true);
        if(i==1){
            MapFragment mapf = new MapFragment();
            frmt = frm.beginTransaction();
             frmt.replace(R.id.fragmentholder, mapf);
            frmt.commit();
        }else if (i==2){
            Fragment2 fr2 = new Fragment2();
            frmt = frm.beginTransaction();
            frmt.replace(R.id.fragmentholder, fr2);
            frmt.commit();
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actbartoggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }else if (id == android.R.id.home){
            if(drawer.isDrawerOpen(navlist)){
                drawer.closeDrawer(navlist);
            }else{
                drawer.openDrawer(navlist);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch(position){
            case 0:

                break;
            case 1:

                loadDefFrag(position);

                break;
            case 2:
                loadDefFrag(position);
                break;
            case 3:
                break;
        }
        drawer.closeDrawer(navlist);
    }
}

在这里你可以看到mapactivity:

  package com.example.matant.test;

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

import com.example.matant.test.R;
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 MapFragment extends FragmentActivity implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

主要布局:

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer"
    >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentholder">

    </FrameLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navlist"
        android:background="#dedede"

        android:layout_gravity="start">

    </ListView>

</android.support.v4.widget.DrawerLayout>

地图布局:

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.matant.test.MapFragment">

    <!-- TODO: Update blank fragment layout -->

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

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

您正尝试使用Activity替换FragmentManager。这里只能使用Fragment

使用MapFragment开始startActivity(),或者MapFragment延伸Fragment

public class MapFragment extends Fragment implements OnMapReadyCallback {

如果你遵循第二种方法,你将不得不在代码中改变其他几行,你需要使用getActivity()

的上下文