【发布时间】:2017-04-09 13:26:26
【问题描述】:
我的片段类出现错误
java.lang.NullPointerException:尝试在 fragment.HomeFragment.onResume 的空对象引用上调用虚拟方法“boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()”
这是我在 HomeFragment 上的部分代码
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition =
savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
} buildGoogleApiClient();
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected()) {
getDeviceLocation();
}
updateMarkers();
}
private synchronized void buildGoogleApiClient() {
GoogleApiClient.Builder builder = new GoogleApiClient.Builder(getActivity());
mGoogleApiClient = builder.build();
//builder.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */);
builder.addConnectionCallbacks(this);
builder.addApi(LocationServices.API);
builder.addApi(Places.GEO_DATA_API);
builder.addApi(Places.PLACE_DETECTION_API);
builder.build();
createLocationRequest();
}
我也实现了
- OnMapReadyCallback
- GoogleApiClient.ConnectionCallbacks
- GoogleApiClient.OnConnectionFailedListener
- 位置监听器
- DirectionFinderListener
有人可以帮我解决这个问题吗?
【问题讨论】:
-
What is NullPointerException and how do I fix it 的可能重复项。我们需要的两个代码中只有一个相关的代码部分:你从来没有告诉我们你在哪里分配了
mGoogleApiAgent。 -
@M.Prokhorov 我编辑了我的帖子。
-
考虑到您添加的部分,我的链接比以前更相关。
标签: java android google-maps android-fragments