为什么 getcontext() 只在一种方法中为 null?



你好,我正在制作一个地图应用程序,该应用程序位于Android Studio的抽屉活动内,一切都很顺利,直到我开始玩片段,因为我在Android中很陌生,我真的不知道发生了什么或问题的原因是什么。

我用地图布局替换了主布局,使其成为我的"主页"布局。然后我添加了一个片段,我可以在其中更改名为"配置"的应用程序的语言,并在抽屉菜单上按下"配置"按钮时调用该片段。

我注意到,即使配置片段取代了地图布局,地图片段中的某些位置方法在后台仍然处于活动状态,但我知道这一点,因为每次应用程序检查权限或更改位置时都会显示 Toast 消息我这样做是为了视觉辅助,所以我知道它的工作原理。

在 2 种情况下,我的应用程序在同一行代码中因相同的错误而崩溃,它恰好是我作为辅助放在那里的 Toast 之一。这对我来说很奇怪,因为所有其他 toast 都工作正常,因为消息正在显示,但是当它在 onLocationChanged 方法上到达该特定 toast 时,会显示下一个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.widget.Toast.<init>(Toast.java:114)
at android.widget.Toast.makeText(Toast.java:277)
at android.widget.Toast.makeText(Toast.java:267)
at com.tesseract.psiclops.zerov2.mapFragment.onLocationChanged(mapFragment.java:172)
at com.google.android.gms.internal.location.zzay.notifyListener(Unknown Source:4)
at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(Unknown Source:8)
at com.google.android.gms.common.api.internal.ListenerHolder$zza.handleMessage(Unknown Source:16)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

因此,触发错误的 2 件事是:

1.- 当我按下后退按钮并且后退堆栈中没有片段(我想(所以它会隐藏(关闭? 最小化?(应用程序然后我再次打开它......它崩溃了。

2.- 当我进入我的配置片段并更改语言时。注意:我用于更改语言的方法会重新启动活动,以便显示语言更改。

我想既然我同时按下后退按钮或调用我的 languaje 方法都关闭了应用程序,因此 getcontext(( 在此之后变为空是有道理的,但为什么其他 toast 显示消息并且仅在该特定行崩溃? 还是特定方法? 我该如何防止这种情况?

注意:当我注释掉 toast 时,一切正常,没有崩溃或任何东西:S,我真的需要了解发生了什么,以便以后可以防止任何问题。

地图片段:

public class mapFragment extends Fragment implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private  GoogleMap mMap;
private GoogleApiClient mApiClient;
private Context mContext;
private OnFragmentInteractionListener mListener;
public mapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
}
private void SetSancrisM(final GoogleMap mMap) {
// Add a marker in Sancris and move the camera
// LatLng sancris = new LatLng(16.736380, -92.638795);
LatLngBounds SanCris = new LatLngBounds(new LatLng(16.720215, -92.684189), new LatLng(16.749950, -92.596649));

//mMap.addMarker(new MarkerOptions().position(sancris).title("Marker in Sancris"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SanCris.getCenter(), 16));
//mMap.setLatLngBoundsForCameraTarget(SanCris);

}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.style_json));
if (!success) {
Toast.makeText(mContext, "Chido3", Toast.LENGTH_SHORT).show();
}
} catch (Resources.NotFoundException e) {
Toast.makeText(mContext, "No cargó el styla", Toast.LENGTH_SHORT).show();
}
//Calls the function that moves the cam to Sancris
SetSancrisM(mMap);

//Location permission conditions
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(mContext, "Failed to get location permission", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
return;
} else {
if (!mMap.isMyLocationEnabled()) {
mMap.setMyLocationEnabled(true);
Toast.makeText(mContext, "Chido", Toast.LENGTH_SHORT).show();
}
}

mApiClient = new GoogleApiClient.Builder(getContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mApiClient.connect();

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 200: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(mContext, "Failed to get permission 2", Toast.LENGTH_SHORT).show();
} else {
mMap.setMyLocationEnabled(true);
Toast.makeText(mContext, "Chido2", Toast.LENGTH_SHORT).show();
}
}
}
}
}

@Override
public void onLocationChanged(Location location) {
if(location==null){
Toast.makeText(mContext, "No Location 4", Toast.LENGTH_SHORT).show();
}else{
LatLng ll= new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate update= CameraUpdateFactory.newLatLngZoom(ll,mMap.getCameraPosition().zoom);
mMap.animateCamera(update);
Toast.makeText(mContext, "Chido4", Toast.LENGTH_SHORT).show(); //This one right here is the one not working
}
}
LocationRequest mLocReq;
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocReq = LocationRequest.create();
mLocReq.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocReq.setInterval(1000);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocReq, this);
Toast.makeText(mContext, "Chido5", Toast.LENGTH_SHORT).show();
}



@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}



//Este código tiene que ver con la comunicación entre fragmentos y actividades, osea déjalo para después

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
mContext=context;
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}


/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}

在onLocationChanged 方法中不起作用的吐司

@Override
public void onLocationChanged(Location location) {
if(location==null){
Toast.makeText(getContext(), "No Location 4", Toast.LENGTH_SHORT).show();
}else{
LatLng ll= new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate update= CameraUpdateFactory.newLatLngZoom(ll,mMap.getCameraPosition().zoom);
mMap.animateCamera(update);
Toast.makeText(getContext(), "Good4", Toast.LENGTH_SHORT).show(); //This one right here is the one not working
}
}

映射片段方法中的所有其他 Toast 工作正常。

如果您需要更多代码,请在此处询问我,我会为您添加它。提前非常感谢!!

在片段中声明上下文变量

private Context mContext;

从 onAttach(( 初始化它

@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;

}

你的吐司将是

Toast.makeText(mContext,getString(R.string.toast) , Toast.LENGTH_LONG).show();

getContext()仅在片段附加到其包含的活动时返回非空值 - 介于onAttach()onDetach()之间。如果你有一个在这些事件之外发生的回调,那么你没有适当地清理你的侦听器。

您应该最早创建侦听器(即呼叫requestLocationUpdates()(是在onAttach()中。

然后,您必须在onDetach()中取消注册侦听器(即调用removeUpdates()(,以防止泄漏您的位置侦听器和整个片段。

通常,您应该仅在屏幕上显示片段时才请求更新。在这种情况下,您将在onStart()中注册并在onStop()中取消注册。

相关内容

最新更新