Google Maps v2在现有片段中的实施

时间:2014-03-24 21:42:56

标签: android google-maps android-fragments

我想知道是否有人可以帮助解决我面临的问题。

我有一个应用程序,主要活动有4个按钮和一个片段,每按一次按钮,片段就会改变,到目前为止一切都很好。

我完全难过将Google Maps v2添加到其中一个片段, *例如,当我按下按钮2时,我希望它在已存在的片段(fragment_place)中加载地图,我似乎无法找到一种方法可以做到这一点以及此处的所有其他问题解决方案对我来说似乎不起作用。*

任何帮助都会受到赞赏,我首先不是一个了不起的程序员,但Android对我来说似乎特别困难。

我已经调整了清单文件,但我不知道如何使用我现有的片段作为地图的“持有者”

主要活动

package com.grim.fragments;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;

public class MainActivity extends Activity {

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

}

public void SelectFrag(View view) {
    Fragment fr = null;

    if (view == findViewById(R.id.button1)) {
        fr = new FragmentOne();
    } else if (view == findViewById(R.id.button2)) {
        fr = new FragmentMap();
    } else if (view == findViewById(R.id.button3)) {
        fr = new FragmentThree();
    } else if (view == findViewById(R.id.button4)) {
        fr = new FragmentFour();
    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();

}

}

活动主要

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/pipboyscreenpsd_nobuttons"
    android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="33dp"
        android:layout_weight="2.00"
        android:background="@android:color/transparent"
        android:onClick="SelectFrag"
        android:text="Status"
        android:textColor="@android:color/holo_blue_bright"
        android:layout_marginTop="20dp" />
</LinearLayout>

<fragment
    android:id="@+id/fragment_place"
    android:name="com.grim.fragments.FragmentHome"
    android:layout_width="664dp"
    android:layout_height="392dp"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="150dp"
    android:layout_marginRight="90dp" />

<Space
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.24" >

    <ImageView
        android:id="@+id/button4"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:onClick="SelectFrag"
        android:layout_toRightOf="@+id/button3"
        android:src="@drawable/pipboybutton_unpressed" />

    <ImageView
        android:id="@+id/button3"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="SelectFrag"
        android:src="@drawable/pipboybutton_unpressed" />

    <ImageView
        android:id="@+id/button2"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_toLeftOf="@+id/button3"
        android:onClick="SelectFrag"
        android:src="@drawable/pipboybutton_unpressed" />

</RelativeLayout>

就像一个例子,我想让按钮2成为地图按钮

package com.grim.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_two, container, false);
}


}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</RelativeLayout>

再次提供任何帮助都会受到赞赏,因为这让我很生气,因为我甚至不知道从哪里开始,通常我对下一步需要做什么有一个想法。

编辑 - 忘记添加我尝试的内容

package com.grim.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;

public class FragmentMap extends Fragment {

    private MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.mapview, container, false);
        mMapView = (MapView) v.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();//needed to get the map to display immediately


        googleMap = mMapView.getMap();

        //Perform any camera updates here

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}



<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />

03-24 22:23:22.330: E/AndroidRuntime(31831): FATAL EXCEPTION: main
03-24 22:23:22.330: E/AndroidRuntime(31831): java.lang.NullPointerException
03-24 22:23:22.330: E/AndroidRuntime(31831):    at maps.e.bf.b(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at eio.onTransact(SourceFile:115)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at     android.os.Binder.transact(Binder.java:310)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onResume(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.google.android.gms.maps.MapFragment$a.onResume(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.google.android.gms.dynamic.a$6.b(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.google.android.gms.dynamic.a.a(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.google.android.gms.dynamic.a.onResume(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.google.android.gms.maps.MapFragment.onResume(Unknown Source)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.Fragment.performResume(Fragment.java:1738)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.os.Handler.handleCallback(Handler.java:725)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.os.Looper.loop(Looper.java:137)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at android.app.ActivityThread.main(ActivityThread.java:5227)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at java.lang.reflect.Method.invokeNative(Native Method)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at java.lang.reflect.Method.invoke(Method.java:511)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-24 22:23:22.330: E/AndroidRuntime(31831):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我终于想出了一种让我的现有片段中的地图运行的方法

public class MapFragment extends Fragment {

MapView map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
    // inflat and return the layout
    View v = inflater.inflate(R.layout.map_fragment, container, false);
    map = (MapView) v.findViewById(R.id.mapView);
    map.onCreate(savedInstanceState);

    return v;
}

@Override
public void onResume() {
    super.onResume();
    map.onResume();
}

@Override
public void onPause() {
    super.onPause();
    map.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    map.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    map.onLowMemory();
}
}

mapview.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mapView" />