Android谷歌地图+滑动菜单,实现

时间:2014-08-15 19:11:28

标签: android google-maps android-fragments

我是编程的初学者,我试图在滑动菜单中实现谷歌地图,在家里,这是片段

<RelativeLayout 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.moictab.decanias.MapActivity$PlaceholderFragment" >

<fragment 

          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"

          />

</RelativeLayout>

这是我想要放置地图的课程

public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        android.app.FragmentTransaction mTransaction = getFragmentManager().beginTransaction();
        SupportMapFragment mFRaFragment = new MapFragment();
        Bundle args = new Bundle();
        args.putDouble("lat", -4.573362);
        args.putDouble("lng", -44.6020426);
        mFRaFragment.setArguments(args);
        mTransaction.add(R.id.map, mFRaFragment);
        mTransaction.commit();


        MapsInitializer.initialize(getApplicationContext());


        return rootView;
    }

}

但我收到这些错误

mTransaction.add(R.id.map, mFRaFragment);

FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int,SupportMapFragment)

MapsInitializer.initialize(getApplicationContext());

对于HomeFragment类型

,未定义getApplicationContext()方法

解决方案是:

public class HomeFragment extends FragmentActivity {

在这个类中一切都保持不错,但是在MainActivity中是不可接受的,因为有扩展Fragment,我已经尝试将其更改为FragmentActivity,但是替换不起作用:

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

message:“FragmentTransaction类型中的方法replace(int,Fragment)不适用于参数(int,FragmentActivity)”

我已经尝试过使用android.support.v4.app.Fragment,但它也不起作用。 我的问题是在导航抽屉中实现谷歌地图android。

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new FindPeopleFragment();
        break;
    case 2:
        fragment = new PhotosFragment();
        break;
    case 3:
        fragment = new CommunityFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        fragment = new WhatsHotFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

我已经看到太多的方法,但它们也不起作用,也是这样:

map = ((SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map)).getMap();

并导入

import android.support.v4.app.FragmentActivity; 在此之前右键单击project-&gt; properties-&gt; buildpath-&gt; java build path - &gt;库..然后点击添加外部罐子

转到

\android-sdks\extras\android\support\v4

and select android-support-v4.jar

1 个答案:

答案 0 :(得分:0)

正如您收到的错误所述:

  

引起:java.lang.IllegalStateException:应用程序的AndroidManifest.xml中的元数据标记没有正确的值。预期4452000但找到0.您必须在元素中包含以下声明:

<meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" />

因此,您需要将此元数据元素添加到应用程序配置单元下的Manifest文件中:

<meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" />