2016-06-29 15 views
1

片面Googleマップと片面Recyclerviewを含む1つのフリップビューを作成しようとしています。 マップフラグメントを呼び出そうとするとエラーが発生します。初期化時にマップがロードされている間は、nullポインタが返されます。Googleマップがカスタムフリップビューで初期化されていません

ファイル1:fragment_left.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> 

ファイル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: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> 

ファイル3:flipscreen.xml

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

Javaファイル ファイル1: flipscreen.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); 
      } 
     } 
    }; 
} 

ファイル2:fragmentLeft.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; 
    } 
} 

ファイル3:FragmentRight.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; 
    } 
} 

私の問題は、それが

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

答えて

1

をnullになってきたfragmentLeft.javaであります私はあなたのコードを見直しました。私はエラーがR.id.mapだと思います、あなたはいない、XMLであなたが@+id/map_fragmentを持っている:

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

ありがとうございますが、私はあなたの変更を試しても、まだ動作しません。 –

1

は私がここに...私のこの問題の解決策を見つけたというのANSです。 私はそれを見つけました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); 
     } 
    } 
    } 
関連する問題