我遇到了这个问题,当用户拒绝在弹出窗口中打开他们的设置时Views
,我的返回 null 。我似乎无法引用. 我想这是因为请求的弹出对话框取代了我的,但我不知道如何在我的. 这是我的尝试:Fragment
Location
Views
Fragment
Location
View
Fragment View
Views
Fragment
private View fragmentView;
private View progressOverlay;
private View noPostsView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (fragmentView == null) {
fragmentView = inflater.inflate(R.layout.posts_tab, container, false);
setupActivity(savedInstanceState);
progressOverlay = fragmentView.findViewById(R.id.progress_overlay);
progressOverlay.setVisibility(View.VISIBLE);
noPostsView = fragmentView.findViewById(R.id.no_posts_layout);
setupLocation();
}
return fragmentView;
}
private void setupLocation() {
Context context = getContext();
if(locationService == null) {
locationService = new LocationService(context, new LocationUpdateListener() {
@Override
public void canReceiveLocationUpdates() {
}
@Override
public void cannotReceiveLocationUpdates() {
createSettingsRequest();
//well we know we cant receive updates so we have to create a settings request
}
//update location to our servers for tracking purpose
@Override
public void updateLocation(Location location) {
if (location != null) {
//Populate Recycler View
databaseQuery.getPublicPosts(location, progressOverlay, fragmentView, getContext());
}
}
@Override
public void updateLocationName(String localityName, Location location) {
locationService.stopLocationUpdates();
}
});
locationService.startUpdates();
}
}
private void createSettingsRequest() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(locationService.mGoogleApiClient
, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(getActivity(), REQUEST_LOCATION);
} catch (IntentSender.SendIntentException e) {
FirebaseCrash.report(e);
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_LOCATION:
switch (resultCode) {
case Activity.RESULT_OK:
break;
case Activity.RESULT_CANCELED:
setupNoPostsView();
break;
}
break;
}
}
private void setupNoPostsView() {
progressOverlay.setVisibility(View.GONE);
noPostsView.setVisibility(View.VISIBLE);
RecyclerView recyclerView = (RecyclerView) fragmentView.findViewById(R.id.rv_posts_feed);
recyclerView.setVisibility(View.GONE);
TextView noPosts = (TextView) noPostsView.findViewById(R.id.no_posts_text);
noPosts.setText(R.string.permission_location_rationale);
}
setupNoPostsView
如果用户单击No
允许,我只是想打电话,但无论出于Location Services
何种原因,当我尝试设置. 关于如何解决这个问题的任何想法?progressOverlay View
null
Visibility = View.GONE
此处更新是第一个答案所要求的我的布局:
片段.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/posts_tab"
tools:showIn="@layout/app_bar_news_feed" tools:context=".news_feed">
<include layout="@layout/include_progress_overlay"/>
<include layout="@layout/no_posts"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rv_posts_feed"
android:visibility="gone"
/>
</FrameLayout>
no_posts.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/no_posts_layout"
android:visibility="gone"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:layout_gravity="center"
android:gravity="center"
android:textAlignment="center"
android:id="@+id/no_posts_text" />
</FrameLayout>