2017-03-24 106 views
3

我有問題加載谷歌地圖在我的片段。一切都開始在launchs活動 「ViatgeDetallViewActivity」 回收者視圖適配器:谷歌地圖在片段android

Context context = mActivity.getApplicationContext(); 
        Intent intent = new Intent(context, ViatgeDetallViewActivity.class); 
        holder.mViatge.getId()); 
        intent.putExtra("desc",p.getDesc()); 
        intent.putExtra("location",p.getLocation()); 
        mActivity.startActivityForResult(intent,ACTIVITY_View); 

這是我DetallViewActivity:

public class ViatgeDetallViewActivity extends AppCompatActivity { 

ViatgeDetallViewFragment fragment; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view); 
    Bundle extras = getIntent().getExtras(); 
    if(extras != null) 
    { 

     String s = getIntent().getStringExtra("desc"); 
     Location l = getIntent().getParcelableExtra("location"); 

     Log.d("myTag", "estoy en el oncreate de detallviewActivity pasando pro extras" + s); 


    } 

     fragment = new ViatgeDetallViewFragment(); 


     getSupportFragmentManager().beginTransaction() 
       .add(R.id.activity_fragment_view, fragment) 
       .commit(); 
    getFragmentManager().beginTransaction().add(R.id.activity_fragment_view, fragment).commit(); 
    } 
} 

隨着我ViatgeDetallViewFragment之中:

public class ViatgeDetallViewFragment extends Fragment implements OnMapReadyCallback { 

private GoogleMap mMap; 
private Marker mMarcadorActual; 

public ViatgeDetallViewFragment() { 
} 
.... 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_view, container, false); 


    SupportMapFragment mapFragment = 
      (SupportMapFragment) 
        getChildFragmentManager().findFragmentById(R.id.fragment_view_map); 
    mapFragment.getMapAsync(this); 


    return rootView; 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 


    //ask for permissions.. 
     mMap.setMyLocationEnabled(true); 


    LatLng sydney = new LatLng(-34, 151); 
    mMarcadorActual = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 

} 

隨着佈局是,Activity_view:

<Linear ... 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/tlbMenuView" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     /> 

    <FrameLayout 
     android:id="@+id/activity_fragment_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

fragment_view:

<LinearLayout android:id="@+id/layout_fragment_view" xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
.. 
    <fragment 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/fragment_view_map" 
     class="com.google.android.gms.maps.MapFragment"></fragment> 

</LinearLayout> 

問題是當我嘗試推出TJE 「ViatgeDetallActivity」,我得到這個在日誌中:

致命異常:主要 過程:com.example.usuari。 myapplication3,PID:6390 了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.example.usuari.myapplication3/com.example.usuari.myapplication3.ViatgeDetallViewActivity}: java.lang.Null PointerException:嘗試調用虛擬方法'null com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' 關於null對象引用 at android.app。 ActivityThread.performLaunchActivity(ActivityThread.java:3254) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) 在 android.app .ActivityThread $ H.handleMessage(ActivityThread.java:1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java.lang.NullPointerException:嘗試在空對象引用 處調用虛擬 方法'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' com.example.usuari.myapplication3.ViatgeDetallViewFragment.onCreateView(ViatgeDetallViewFragment.java:82) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:2074) 在 android.support.v4.app .FragmentManagerImpl.moveToState(FragmentManager.java:1104) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286) 在 android.support.v4.app.BackStackRecord.run(BackStackRecord。的java:758) 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671) 在 android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 在 機器人(Activity.java:6915).support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265) at android.app.Activity.performStart(Activity.java:6915) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3217) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) at android.app.ActivityThread.access $ 1100(ActivityThread.java:222) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java: 1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.jav a:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 03-24 02:55:53.871 6390-7362/com.example.usuari.myapplication3 W/DynamiteModule:Local找不到 com.google.android.gms.google證書的模塊描述符類。 03-24 02:55:53.881 6390-7362/com.example.usuari.myapplication3 I/DynamiteModule:考慮本地模塊 com.google.android.gms.googlecertificates:0和遠程模塊 com.google.android。 gms.googlecertificates:二月3日至24日02:55:53.881 6390-7362/com.example.usuari.myapplication3 I/DynamiteModule:選定com.google.android.gms.googlecertificates的 遠程版本,版本

= 2 03-24 02:55:53.881 6390-7362/com.example.usuari.myapplication3 W/ResourcesManager:getTopLevelResources: /data/user/0/com.google.android.gms/app_chimera/m/00000005/ DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk /1.0在com.example.usuari.myapplication3中運行包的rsrc com.example.usuari.myapplication3 03-24 02:55:53.891 6390-7362/com.example.usuari.myapplication3 D/ResourcesManager:對於 用戶0個新疊加取空值03-24 02:55:53.891 6390-7362/com.example.usuari.myapplication3 W/ResourcesManager: getTopLevelResources: /data/user/0/com.google.android.gms/app_chimera/米/ 00000005/DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk /1.0在包的com.example.usuari.myapplication3 RSRC運行 com.example.usuari.myapplication3

+0

如果我理解正確的,你想我要更改的元素片段佈局的SupportMapFragment,但是Android工作室它不承認,作爲一個有效的類型! – Asdemuertes

回答

5

您的mapFragment爲空。試試這個:

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
super.onViewCreated(view, savedInstanceState); 
MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_view_map); 
}