gpt4 book ai didi

android - Android fragment 中的谷歌地图

转载 作者:行者123 更新时间:2023-12-02 16:17:03 25 4
gpt4 key购买 nike

我在 fragment 中加载谷歌地图时遇到问题。一切都从启动 Activity “ViatgeDetallViewActivity”的回收器 View 适配器开始:

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

这是我的 DetailViewActivity :

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>

当我尝试启动“ViatgeDetallActivity”时,问题就出现了,我在日志中得到了这个:

FATAL EXCEPTION: main Process: com.example.usuari.myapplication3, PID: 6390 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usuari.myapplication3/com.example.usuari.myapplication3.ViatgeDetallViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254) at 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) 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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at com.example.usuari.myapplication3.ViatgeDetallViewFragment.onCreateView(ViatgeDetallViewFragment.java:82) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671) at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) at android.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) at 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)  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)  03-24 02:55:53.871 6390-7362/com.example.usuari.myapplication3 W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found. 03-24 02:55:53.881 6390-7362/com.example.usuari.myapplication3 I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2 03-24 02:55:53.881 6390-7362/com.example.usuari.myapplication3 I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version

= 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 running in com.example.usuari.myapplication3 rsrc of package com.example.usuari.myapplication3 03-24 02:55:53.891 6390-7362/com.example.usuari.myapplication3 D/ResourcesManager: For user 0 new overlays fetched Null 03-24 02:55:53.891 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 running in com.example.usuari.myapplication3 rsrc of package com.example.usuari.myapplication3

最佳答案

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

关于android - Android fragment 中的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42990121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com