2016-08-20 7 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

使用:XMLファイルの使用内部

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

マニフェスト内部
<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を忘れないでください、あなたはそれをgénérateしなければなりません、このリンクを参照してください:console.developers.google.com

私はそれがあなたを助けることを望みます。

+0

アミールありがとう、私はレイアウトを使用してアプリケーションが動作しているが、それは単一のレイアウトに変更し、残りは断片化したい。地図は私に難しさを与えてくれる唯一の断片です。 – user1636612

0

新しいエミュレータを作成した後、getChildFragmentManager()コールを使用して動作し始めました。答えをありがとう。

0

私は解決策は、あなたのパッケージにWorkaroundMapFragmentこのクラスを追加することですグラムライブラリ内のそれを見つけることはありませんSupportMapFragmentと同じ問題があります:あなたはこのように、このクラスを呼び出す

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); 
         } 
        }); 

     } 
関連する問題