2016-08-20 140 views
1

我已经看过几个类似问题的发布解决方案,但他们都没有工作。在下面的代码中findFragmentById总是返回null。我从另一个片段调用MapLocationFragment。无法获取Android SupportMapFragment

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
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 MapLocationFragment extends Fragment implements OnMapReadyCallback { 
    private String tag; 

    private OnFragmentInteractionListener mListener; 

    public MapLocationFragment() {} // Required 

    private FragmentActivity context; 
    private GoogleMap mMap; 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     Log.d(tag, "onAttach"); 
     this.context = (FragmentActivity)context; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     tag = this.getClass().getSimpleName(); 
     Log.d(tag, "onCreate"); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     Log.d(tag, "onCreateView"); 

     View view = inflater.inflate(R.layout.fragment_map, container, false); 

     SupportMapFragment frag1 = (SupportMapFragment) getChildFragmentManager() 
       .findFragmentById(R.id.mapFragment); 

     mapFragment.getMapAsync(this); 

     return view; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     // ... 
    } 
} 

而片段布局(尝试都片段和的FrameLayout)

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/mapFragment" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.spireon.fai.fleetlocate.MapLocationFragment"> 
</fragment> 

回答

0

首先,存在用于FrameLayout没有android:name属性。这是<fragment>的属性。

因此,让我们假装代替<FrameLayout>,而不是<fragment>,而是在您的问题中提示。

在这种情况下,您的问题是您在尝试查找R.id.mapFragment,然后膨胀包含R.id.mapFragment的布局。在使用findViewById()findFragmentById()访问该布局的内容之前,需要对布局进行充气。

+0

感谢您指出我在查找片段引用(我相应地修改了帖子中的代码)之前未扩大视图的错误。但是,findFragmentById()仍然返回null。 – user1636612

0

使用:

MapFragment mapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

XML文件里面使用:

<fragment 
      android:id="@+id/map" 
      android:name="dz.android.grcm.WorkaroundMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="300dp" 
      android:fragmentExitTransition="@layout/yourXmlFile" /> 

您的清单中:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="packageName" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="17" 
     android:targetSdkVersion="23" /> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 

    <permission 
     android:name="packageName.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 

    <uses-permission android:name="packageName.permission.MAPS_RECEIVE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="your_APIKEY_"/> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity android:name="packageName.Time_Picker" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

对我来说它的工作原理与GOOGLE_PLAY_SERVICES_VERSION:7095000和Android API 23 不要忘记API_KEY,你必须安排它,参考这个链接:console.developers.google.com

试试吧,我希望它可以帮助你。

+0

Amir谢谢,我有应用程序使用布局,但想要将其更改为单个布局,其余部分为碎片。地图是唯一给我带来困难的片段。 – user1636612

0

创建新仿真器后,它开始使用getChildFragmentManager()调用工作。感谢您的答案。

0

我有SupportMapFragment同样的问题,我不觉得这里面库的克数的解决方案是WorkaroundMapFragment添加这个类到你的包:

package dz.android.grcm; 

import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.FrameLayout; 

import com.google.android.gms.maps.MapFragment;; 

public class WorkaroundMapFragment extends MapFragment { 
    private OnTouchListener mListener; 

    @SuppressWarnings("deprecation") 
    @Override 
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) { 
     View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance); 

     TouchableWrapper frameLayout = new TouchableWrapper(getActivity()); 

     frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 

     ((ViewGroup) layout).addView(frameLayout, 
       new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 

     return layout; 
    } 

    public void setListener(OnTouchListener listener) { 
     mListener = listener; 
    } 

    public interface OnTouchListener { 
     public abstract void onTouch(); 
    } 

    public class TouchableWrapper extends FrameLayout { 

     public TouchableWrapper(Context context) { 
      super(context); 
     } 

     @Override 
     public boolean dispatchTouchEvent(MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        mListener.onTouch(); 
        break; 
       case MotionEvent.ACTION_UP: 
        mListener.onTouch(); 
        break; 
      } 
      return super.dispatchTouchEvent(event); 
     } 
    } 
} 

然后调用这个类像这样:

public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.inscriptionmed); 
     MapFragment mapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     // *************Fix Scroll Map******************************* 
     if (map == null) { 
      map = ((WorkaroundMapFragment) getFragmentManager() 
        .findFragmentById(R.id.map)).getMap(); 
      map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      map.getUiSettings().setZoomControlsEnabled(true); 
      map.setMyLocationEnabled(true); 
      mScrollView = (ScrollView) findViewById(R.id.partentScroll); 

      ((WorkaroundMapFragment) getFragmentManager().findFragmentById(
        R.id.map)) 
        .setListener(new WorkaroundMapFragment.OnTouchListener() { 
         @Override 
         public void onTouch() { 
          mScrollView 
            .requestDisallowInterceptTouchEvent(true); 
         } 
        }); 

     }