谷歌地图未在自定义Flipview中初始化

时间:2016-06-29 14:06:57

标签: java android google-maps

我正在尝试创建一个包含Google地图一侧和一侧Recyclerview的flipview。 我尝试调用Map片段时收到错误。当map必须在初始化时加载时,它会给出nullpointer。

文件1:fragment_left.xml

class Debit(object):
  currency = "SEK"

  def __init__(self,date,tag,ammount):
    self.date = date  # use of self.date calls the setter to set self._date with input checking
    self.tag = tag
    self.ammount = ammount 

  @property
  def date(self):  # property getter
    return self._date  # provide attribute

  @date.setter
  def date(self, d):  # property setter
    if len(d) == 0: 
      raise Exception("Date cannot be empty")
    self._date = d  # set attribute

文件2:fragment_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/transparent">


    <fragment
        android:id="@+id/map_fragment"
        android:layout_below="@+id/header"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_above="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

文件3:flipscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical" >


    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:background="@mipmap/back"
        android:layout_above="@+id/footer"/>

</LinearLayout>

Java文件 文件1:flipscreen.java:

<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=".flipscreen" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@mipmap/gradiant"
        android:layout_alignParentTop="true"
        android:padding="6dp">

        <TextView
            android:id="@+id/activity_registration_txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="@string/events"/>
        <ImageView
            android:id="@+id/listOfEvent"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/menu"/>
    </RelativeLayout>
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_below="@+id/header"
        android:layout_above="@+id/footer"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
    </FrameLayout>
</RelativeLayout>

文件2:fragmentLeft.java:

public class flipscreen extends FragmentActivity {

    private boolean showingBack;
    private FragmentLeft left = new FragmentLeft();
    private FragmentRight right = new FragmentRight();
    private Context context;
    private Handler handler;
    private FlipAnimation flipAnimation;
    private FlipAnimation backFlip;
    private GoogleMap map;
    private FragmentManager mFragmrg;
    List<Event> lsEvent=new ArrayList<Event>();
    private double latitude = 0;
    private double longitude = 0;
    FrameLayout mapLayout;
    ImageView flipView;
    ImageButton btnAddEvent;
    String strUserId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flipscreen);

        context = this;
        handler = new Handler(getMainLooper());
        mapLayout = (FrameLayout)findViewById(R.id.fragment_container);
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, right, "fragmentRight").commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, left, "fragmentLeft").commit();

        flipView=(ImageView) findViewById(R.id.listOfEvent);
        btnAddEvent=(ImageButton)findViewById(R.id.btnAddEvent);
        btnAddEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!(strUserId.equals(""))){
                    startActivity(new Intent(flipscreen.this,AddEventActivity.class));
//                    finish();
                }else{
                    startActivity(new Intent(flipscreen.this,LoginActivity.class));
                }
            }
        });
        flipView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                flipAnimation = new FlipAnimation(left.getView(), right.getView());
                backFlip = new FlipAnimation(left.getView(), right.getView());
                handler.removeCallbacks(rotate);
                handler.postDelayed(rotate, 100);
            }

        });

    }


    private Runnable rotate = new Runnable() {

        @Override
        public void run() {
            if (!showingBack) {
                left.getView().startAnimation(flipAnimation);
                right.getView().startAnimation(flipAnimation);
//                Toast.makeText(context, "flip", Toast.LENGTH_LONG).show();
                flipView.setImageResource(R.mipmap.nav_map);
                showingBack = true;
            } else {
                showingBack = false;
                backFlip.reverse();
//                Toast.makeText(context, "backflip", Toast.LENGTH_LONG).show();
                left.getView().startAnimation(backFlip);
                right.getView().startAnimation(backFlip);
                flipView.setImageResource(R.mipmap.menu);
            }
        }
    };
}

文件3:FragmentRight.java:

public class FragmentLeft extends Fragment {
    private GoogleMap map;
    private FragmentManager mFragmrg;
    List<Event> lsEvent=new ArrayList<Event>();
    private double latitude = 0;
    private double longitude = 0;
    private SupportMapFragment mMapFragment;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View flipMap =inflater.inflate(R.layout.fragment_left, container,false);

        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
//                // Add a marker in Sydney and move the camera
                LatLng sydney = new LatLng(latitude, longitude);
                map.clear();
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 8);
                map.animateCamera(cameraUpdate);
                System.out.println("Latitude "+latitude+ " \n Longitude "+longitude);
            }
        });

        return  flipMap;
    }
}

我的问题是在fragmentLeft.java中它变为空

public class FragmentRight extends Fragment {
    private static final String TAG ="NearByActivityList" ;

    RecyclerView recyclerView;
    List<Event> lsEvent=new ArrayList<Event>();
    protected GoogleApiClient mGoogleApiClient;
    protected Location mLastLocation;
    ImageView listOfEvent;
    double latitude = 0;
    double longitude = 0;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View fillData =inflater.inflate(R.layout.fragment_right, container,false);
        recyclerView = (RecyclerView) fillData.findViewById(R.id.list);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (3 - position % 3);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return  fillData;
    }
}

2 个答案:

答案 0 :(得分:1)

我已经审核了您的代码,我认为错误是R.id.map,您还没有,@+id/map在xml中@+id/map_fragment

SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_fragment)
mapFragment.getMapAsync(new OnMapReadyCallback() {

答案 1 :(得分:1)

我找到了解决问题的方法..这就是问题的答案。 我在https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064链接中找到了它。

FragmentLeft.java : 
public class FragmentLeft extends Fragment {
    private GoogleMap map;
    GPSTracker gps;
    private SupportMapFragment fragment;
    List<Event> lsEvent=new ArrayList<Event>();
    private double latitude = 0;
    private double longitude = 0;
    private GoogleMap mMap;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View flipMap =inflater.inflate(R.layout.fragment_left, container,false);
        gps = new GPSTracker(getActivity());
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();

        return  flipMap;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_fragment);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_fragment, fragment).commit();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (map == null) {
            map = fragment.getMap();
            System.out.println("Latitude : = "+latitude+" Longitude : ="+longitude);
            map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
            LatLng sydney = new LatLng(latitude, longitude);
            map.clear();
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12);
            map.animateCamera(cameraUpdate);
        }
    }
    }