【问题标题】:Google Maps v2 Implementation into Existing FragmentGoogle Maps v2 在现有片段中的实现
【发布时间】:2014-03-25 20:29:47
【问题描述】:

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

我有一个应用,我的主要活动有 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)

【问题讨论】:

    标签: android google-maps android-fragments


    【解决方案1】:

    我终于找到了让地图在现有片段中运行的方法

    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" />
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2013-06-03
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      相关资源
      最近更新 更多