gpt4 book ai didi

java - GoogleApiClient 空指针异常

转载 作者:太空狗 更新时间:2023-10-29 13:20:01 27 4
gpt4 key购买 nike

我尝试学习 android 开发,现在我正在开发一个具有融合位置服务的应用程序。

当我想放置 GoogleMap fragment 、带有经纬度的 Textviews 和其他一些小部件时,我得到了一个 RunFragment.java 类。首先,我想连接到 GoogleApiClient,我创建了一个新类来完成所有这些工作,并让 RunFragment.java 保持清晰。 (这是一个好主意吗??)当我尝试运行我的应用程序时出现此错误:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at com.runtheworld.roman.runtheworld.GoogleAPI.buildGoogleApiClient(GoogleAPI.java:28)
at com.runtheworld.roman.runtheworld.RunFragment.onCreate(RunFragment.java:39)
at android.app.Fragment.performCreate(Fragment.java:2031)

有相关类:

RunFragment.java

package com.runtheworld.roman.runtheworld;

import android.app.Fragment;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



public class RunFragment extends Fragment {

private static View view;

// TextViews
TextView lat,lon;

// GoogleApi
private GoogleAPI mGoogleApiClient = new GoogleAPI(getActivity());



// Default Constructor
public RunFragment() {
}


@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mGoogleApiClient.buildGoogleApiClient();
}
@Override
public void onStart(){
super.onStart();
mGoogleApiClient.Connect();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.run_fragment, container, false);
} catch (InflateException e) {
// Map already created
}

lat = (TextView)view.findViewById(R.id.lat);
lon = (TextView)view.findViewById(R.id.lon);
lat.setText(String.valueOf(mGoogleApiClient.getLat()));
lon.setText(String.valueOf(mGoogleApiClient.getLon()));


return view;
}




}

GoogleAPI.java

package com.runtheworld.roman.runtheworld;


import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

public class GoogleAPI implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Context context;
private GoogleApiClient mGoogleApiClient;
private boolean isConnected;
// Location
Location mLastLocation;

public GoogleAPI(Context c) {
context = c;
}


protected synchronized void buildGoogleApiClient() {
Log.d("MYTAG", "BUILDING GOOGLE API");
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

public boolean Connect(){
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
else
Log.d("MYTAG","GoogleApiClient is already connected!");
return isConnected;
}

@Override
public void onConnected(Bundle bundle) {
Log.d("MYTAG", "GOOGLE API CONNECTED!");
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
}
}

public double getLat(){
return mLastLocation.getLatitude();
}
public double getLon(){
return mLastLocation.getLongitude();
}
@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

有人能帮忙吗?

最佳答案

听起来您最初的问题已在评论中得到解决,这里是您完成功能的方法。

首先,要修复 NPE,请确保 mLocation 在引用它之前不为 null:

public double getLat(){
double retVal = 0d;
if (mLastLocation != null){
retVal = mLastLocation.getLatitude();
}
return retVal;
}
public double getLon(){
double retVal = 0d;
if (mLastLocation != null){
retVal = mLastLocation.getLongitude();
}
return retVal;
}

下一个问题是 getLastLocation() 返回 null 的可能性很高。

解决这个问题的最佳方法是注册一个位置监听器。

GoogleAPI 声明为位置监听器:

public class GoogleAPI  implements GoogleApiClient.ConnectionCallbacks,    GoogleApiClient.OnConnectionFailedListener, LocationListener{

private Context context;
private GoogleApiClient mGoogleApiClient;
private boolean isConnected;

然后在 onConnected() 中注册一个监听器(参见 documentation :了解如何设置 minTimefastestTime 的值,和 distanceThreshold):

        @Override
public void onConnected(Bundle bundle) {
Log.d("MYTAG", "GOOGLE API CONNECTED!");
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation == null) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(minTime);
mLocationRequest.setFastestInterval(fastestTime);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(distanceThreshold);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

}

然后添加位置改变回调:

 @Override
public void onLocationChanged(Location location) {

mLastLocation = location;

}

关于java - GoogleApiClient 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com