我有一个活动,可以从我的自定义片段中显示googleMap。每个东西都显示正确,但是当我点击此Activity的Back Button时,会出现一条android错误警告消息和'java.lang.RuntimeException:无法销毁活动(... myActivity}:java.lang.NullPointerException'。logcat没有任何指向我的代码中的任何地方,我找不到问题的根源。
这里是logcat:
06-04 06:41:04.206 30479-30479/xxx E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to destroy activity {xxx.InfoActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3624)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3642)
at android.app.ActivityThread.access$1200(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.google.maps.api.android.lib6.d.ai.d(Unknown Source)
at com.google.android.gms.maps.internal.x.onTransact(SourceFile:129)
at android.os.Binder.transact(Binder.java:310)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onDestroyView(Unknown Source)
at com.google.android.gms.maps.MapFragment$zza.onDestroyView(Unknown Source)
at com.google.android.gms.dynamic.zza.onDestroyView(Unknown Source)
at com.google.android.gms.maps.MapFragment.onDestroyView(Unknown Source)
at android.app.Fragment.performDestroyView(Fragment.java:1893)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1868)
at android.app.Activity.performDestroy(Activity.java:5290)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1112)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3611)
这是我的活动代码:
public class InfoActivity extends AppCompatActivity {
Toolbar actionBarToolBar;
CityMapFragment cityMapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_info);
actionBarToolBar = (Toolbar)findViewById(R.id.event_info_toolbar);
setSupportActionBar(actionBarToolBar);
actionBarToolBar.setNavigationIcon(R.mipmap.ic_action_direction);
actionBarToolBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
cityMapFragment = CityMapFragment.getNewInstance(savedInstanceState);
ft.add(R.id.layoutMapEvent, cityMapFragment);
ft.commit();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
cityMapFragment.onPause();
}
@Override
protected void onStop() {
super.onStop();
cityMapFragment.onStop();
}
@Override
protected void onResume() {
super.onResume();
cityMapFragment.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
cityMapFragment.onDestroy();
}
}
这里是我的CityMapFragment代码:
public class CityMapFragment extends Fragment实现OnMapReadyCallback {
public static CityMapFragment getNewInstance(Bundle savedInstanceState)
{
CityMapFragment cityMapFragment = new CityMapFragment();
cityMapFragment.onCreate(savedInstanceState);
return cityMapFragment;
}
public GoogleMap googleMap;
public MapFragment mapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_fragment_layout, container, false);
mapInit(savedInstanceState);
return v;
}
@Override
public void onMapReady(GoogleMap _googleMap) {
this.googleMap = _googleMap;
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(getActivity(), "TAP", Toast.LENGTH_SHORT).show();
}
});
}
private void mapInit(Bundle savedInstanceState)
{
if(googleMap == null)
{
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()))
{
case ConnectionResult.SUCCESS:
MapsInitializer.initialize(getActivity());
mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.mapFragmentID);
mapFragment.onCreate(savedInstanceState);
if(mapFragment != null)
{
mapFragment.getMapAsync(this);
}
Toast.makeText(getActivity(), "SUCCES", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
}
/*if(googleMap != null)
{
mapSetting();
}*/
}
public void setMarkerAndFocus(LatLng latlg, String text) {
googleMap.addMarker(new MarkerOptions().position(latlg).title(text));
focusLocation(latlg);
}
public void setMarker(LatLng latlg, String text)
{
}
public void focusLocation(LatLng latlg)
{
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latlg, 15);
googleMap.animateCamera(cameraUpdate);
}
@Override
public void onResume() {
super.onResume();
mapFragment.onResume();
}
@Override
public void onPause() {
super.onPause();
mapFragment.onPause();
}
@Override
public void onStop() {
super.onStop();
mapFragment.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapFragment.onLowMemory();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
mapFragment.onDestroy();
}
}
我认为问题在于
ft.add(R.id.layoutMapEvent, cityMapFragment);
ft.commit();
因为当我删除此代码时,没有抛出java.lang.NullPointerException。但我想在我的Activity上显示地图,我无法从我的Activity中删除它。 我需要2天时间,但我无法正确解决。
非常感谢你帮助!!!!
答案 0 :(得分:0)
尝试从CityMapFragment中删除onDestroyView()
方法。当您的Activity被销毁时,您的Fragment也会被自动销毁。因此,没有必要。