Android getActivity()返回null—第二次打开片段时
当我按下按钮时,Android getActivity()返回null—第二次打开片段时,android,android-fragments,google-maps-api-3,Android,Android Fragments,Google Maps Api 3,当我按下按钮时,ShowActivities片段被实例化。我第一次按它时,一切正常。这是我在logCat中得到的: 11-16 23:41:12.560 25874-25874/name.company.newapp E/onCreate: getActivity() == null: false 11-16 23:41:12.560 25874-25874/name.company.newapp E/onCreate: Hash: 1036405628 11-16 23:41:12.570 2
ShowActivities
片段被实例化。我第一次按它时,一切正常。这是我在logCat中得到的:
11-16 23:41:12.560 25874-25874/name.company.newapp E/onCreate: getActivity() == null: false
11-16 23:41:12.560 25874-25874/name.company.newapp E/onCreate: Hash: 1036405628
11-16 23:41:12.570 25874-25874/name.company.newapp E/onCreateView: getActivity() == null: false
11-16 23:41:12.570 25874-25874/name.company.newapp E/onCreateView: Hash: 1036405628
11-16 23:41:12.630 25874-25874/name.company.newapp E/onConnected: getActivity() == null: false
11-16 23:41:12.630 25874-25874/name.company.newapp E/onConnected: Hash: 1036405628
一切正常。我按下后退键并返回到上一个片段。现在我再次单击按钮打开ShowActivities
fragment,现在logCat返回的是:
11-16 23:41:13.890 25874-25874/name.company.newapp E/onCreate: getActivity() == null: false
11-16 23:41:13.890 25874-25874/name.company.newapp E/onCreate: Hash: 252302789
11-16 23:41:13.890 25874-25874/name.company.newapp E/onCreateView: getActivity() == null: false
11-16 23:41:13.890 25874-25874/name.company.newapp E/onCreateView: Hash: 252302789
11-16 23:41:13.900 25874-25874/name.company.newapp E/onConnected: getActivity() == null: true
11-16 23:41:13.900 25874-25874/name.company.newapp E/onConnected: Hash: 1036405628
onConnected
在同一个片段上被调用,尽管onCreate正在创建另一个片段
表演活动片段
public class ShowActivities extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
public ShowActivities() {
}
private static GoogleApiClient googleClient;
private Location mLastLocation;
private PendingResult<LocationSettingsResult> result;
private LocationRequest locationRequest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("onCreate"," "+(getActivity() == null));
if (googleClient==null) {
googleClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Log.e("onCreateView"," "+(getActivity() == null));
// Defines the xml file for the fragment
View view = inflater.inflate(R.layout.upload_activity_fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onConnected(Bundle bundle) {
Log.e("onConnected"," "+(getActivity() == null));
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(googleClient, builder.build());
if (result != null) {
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
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 optionsDialog.
try {
// Show the optionsDialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
if (status.hasResolution()) {
status.startResolutionForResult(getActivity(), 1000);
Log.e("location", "Resolution");
}
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
Log.e("location", "Resolution2");
}
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 optionsDialog.
break;
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == Activity.RESULT_OK) && (requestCode == 1000)) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleClient);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.e("location", "finally");
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
if (googleClient!=null) {
googleClient = null;
}
}
}
公共类ShowActivities扩展片段实现
GoogleAppClient.ConnectionCallbacks,GoogleAppClient.OnConnectionFailedListener,LocationListener{
公众展览活动(){
}
私有静态谷歌客户端;
私人场所;
私人结果;
私人位置请求位置请求;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.e(“onCreate”,“+(getActivity()==null));
if(googleClient==null){
googleClient=新的GoogleAppClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
super.onCreateView(充气机、容器、保存状态);
Log.e(“onCreateView”,“”+(getActivity()==null));
//定义片段的xml文件
视图=充气机。充气(R.layout.upload\u activity\u片段,容器,false);
返回视图;
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
}
@凌驾
未连接的公共空间(捆绑包){
Log.e(“onConnected”,“”+(getActivity()==null));
locationRequest=locationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
结果=LocationServices.SettingsApi.checkLocationSettings(googleClient,builder.build());
如果(结果!=null){
result.setResultCallback(新的ResultCallback(){
@凌驾
公共无效onResult(LocationSettingsResult LocationSettingsResult){
最终状态状态=locationSettingsResult.getStatus();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足所有位置设置。客户端可以初始化位置
//请求在这里。
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。但可以通过显示用户来修复
//选项对话框。
试一试{
//通过调用startResolutionForResult()显示选项对话框,
//并在onActivityResult()中检查结果。
if(status.hasResolution()){
status.startResolutionForResult(getActivity(),1000);
Log.e(“位置”、“决议”);
}
}catch(IntentSender.sendtintentexe){
//忽略错误。
日志e(“位置”、“决议2”);
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不满意。但是,我们无法修复此问题
//设置,因此我们不会显示选项对话框。
打破
}
}
});
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if((resultCode==Activity.RESULT_OK)&&(requestCode==1000)){
mLastLocation=LocationServices.FusedLocationApi.getLastLocation(谷歌客户端);
}
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
@凌驾
已更改位置上的公共无效(位置){
Log.e(“位置”、“最终”);
}
@凌驾
恢复时公开作废(){
super.onResume();
}
@凌驾
公共无效暂停(){
super.onPause();
if(googleClient!=null){
googleClient=null;
}
}
}
这个问题解决了。我修改了onPause,使googleClient在退出片段后为null 首先尝试通过标记在占位符中获取片段实例,如果没有先前的输入,则创建一个实例
Fragment ShowActivities = getFragmentManager().getFragmentByTag(YOUR_TAG);
if (showActivities == null) {
showActivities = new ShowActivities();
getFragmentManager().beginTransaction()
.replace(R.id.your_placeholder, showActivities, YOUR_TAG)
.addToBackStack(null)
.commit();
}
首先尝试通过标记在占位符中获取片段实例,如果没有以前的实例,则创建一个实例
Fragment ShowActivities = getFragmentManager().getFragmentByTag(YOUR_TAG);
if (showActivities == null) {
showActivities = new ShowActivities();
getFragmentManager().beginTransaction()
.replace(R.id.your_placeholder, showActivities, YOUR_TAG)
.addToBackStack(null)
.commit();
}
client = new NightAssetRestClient(getActivity());
locationQueries = new LocationQueries(getActivity());
if (googleClient==null) {
googleClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}