2014-03-24 41 views
1

我在想,如果有人能幫我解決一個我面臨的問題。谷歌地圖v2實施到現有片段

我有一個應用程序,我的主要活動有4個按鈕和一個片段,每當你按下一個按鈕片段的變化,到目前爲止這麼好。

然而,我將Google地圖v2添加到其中一個片段,*,例如當我按下按鈕2時我完全難倒了我希望它在已存在的片段(fragment_place)中加載地圖,我似乎無法找到方式來做到這一點,在這裏的其他所有問題的解決方案似乎並沒有爲我工作。*

任何幫助將不勝感激,我不是一個了不起的程序員在首位,但Android似乎特別我自己很難。

我已經調整了manifest文件,但我不知道該如何使用自己現有的片段作爲地圖「持有者」

主要活動

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) 

回答

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