我一直在尝试将Google地图实现为一个viewpager片段,我已经遵循了许多不同的教程,但每次都惨遭失败。如果有人有任何煽动如何得到这样的工作,我会很高兴!以下是我使用的源代码:
的Src / MainActivity.java
public class MainActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
public static FragmentManager fragmentManager;
Context Map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout._layout);
inistialisePaging();
//initialising the object of the FragmentManager.
fragmentManager = getSupportFragmentManager();
}
的Src / Map.java
package joe.wit.app.locophoto;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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 Map extends Fragment {
private static GoogleMap mMap;
private static Double latitude, longitude;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container == null){
return null;
}
latitude = 26.78;
longitude = 72.56;
setUpMapIfNeeded(); // For setting up the MapFragment
return (RelativeLayout) inflater.inflate(R.layout.map_layout,
container, false);
}
/***** Sets up the map if it is possible to do so *****/
public static void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) MainActivity.fragmentManager
.findFragmentById(R.id.location_map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
/**
* This is where we can add markers or lines, add listeners or move the
* camera.
* <p>
* This should only be called once and when we are sure that {@link #mMap}
* is not null.
*/
private static void setUpMap() {
// For showing a move to my loction button
mMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Home").snippet("Home Address"));
// For zooming automatically to the Dropped PIN Location
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
longitude), 12.0f));
}
/**** The mapfragment's id must be removed from the FragmentManager
**** or else if the same it is passed on the next time then
**** app will crash ****/
@Override
public void onDestroyView() {
super.onDestroyView();
if (mMap != null) {
MainActivity.fragmentManager.beginTransaction()
.remove(MainActivity.fragmentManager.findFragmentById(R.id.location_map)).commit();
mMap = null;
}
}
}
RES / map_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:orientation="vertical" >
<fragment
android:id="@+id/location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
LOGCAT错误
02-26 13:34:25.176: E/AndroidRuntime(9065): FATAL EXCEPTION: main
02-26 13:34:25.176: E/AndroidRuntime(9065): Process: joe.wit.app.locophoto, PID: 9065
02-26 13:34:25.176: E/AndroidRuntime(9065): java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
02-26 13:34:25.176: E/AndroidRuntime(9065): at joe.wit.app.locophoto.Map.setUpMapIfNeeded(Map.java:52)
02-26 13:34:25.176: E/AndroidRuntime(9065): at joe.wit.app.locophoto.Map.onCreateView(Map.java:36)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.View.measure(View.java:17525)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.View.measure(View.java:17525)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.View.measure(View.java:17525)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
02-26 13:34:25.176: E/AndroidRuntime(9065): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.View.measure(View.java:17525)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
02-26 13:34:25.176: E/AndroidRuntime(9065): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2629)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.View.measure(View.java:17525)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2102)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1218)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.Choreographer.doCallbacks(Choreographer.java:596)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.Choreographer.doFrame(Choreographer.java:557)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.os.Handler.handleCallback(Handler.java:739)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.os.Handler.dispatchMessage(Handler.java:95)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.os.Looper.loop(Looper.java:155)
02-26 13:34:25.176: E/AndroidRuntime(9065): at android.app.ActivityThread.main(ActivityThread.java:5696)
02-26 13:34:25.176: E/AndroidRuntime(9065): at java.lang.reflect.Method.invoke(Native Method)
02-26 13:34:25.176: E/AndroidRuntime(9065): at java.lang.reflect.Method.invoke(Method.java:372)
02-26 13:34:25.176: E/AndroidRuntime(9065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
02-26 13:34:25.176: E/AndroidRuntime(9065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
非常感谢任何帮助 问候 乔
答案 0 :(得分:0)
我不敢编译(不可动摇的类型:android.app.Fragment
vs com.google.android.gms.maps.SupportMapFragment
)。
这对我有用:
mMap = ((SupportMapFragment) ((FragmentActivity) getActivity()).getSupportFragmentManager().findFragmentById(R.id.location_map)).getMap();
答案 1 :(得分:0)
尝试使用 getChildFragmentManager()而不是 getFragmentManager()。
像:
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();