gpt4 book ai didi

android - Erron 在 Android 中使用 GoogleApiClient 获取最后已知位置

转载 作者:行者123 更新时间:2023-11-30 01:02:04 33 4
gpt4 key购买 nike

尝试获取设备的最后位置

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

GoogleApiClient mGoogleApiClient;
TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = (TextView) findViewById(R.id.text_view);
setContentView(R.layout.activity_main);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}


}

@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}

@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double lat = mLastLocation.getLatitude();

textView.setText(String.valueOf(lat));
}
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

这些是错误

09-01 12:16:06.385 24018-24018/mzkhan.assignmenttask E/AndroidRuntime: FATAL EXCEPTION: main Process: mzkhan.assignmenttask, PID: 24018 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at mzkhan.assignmenttask.MainActivity.onConnected(MainActivity.java:70) at com.google.android.gms.common.internal.zzl.zzo(Unknown Source) at com.google.android.gms.internal.zzpy.zzm(Unknown Source) at com.google.android.gms.internal.zzpw.zzapp(Unknown Source) at com.google.android.gms.internal.zzpw.onConnected(Unknown Source) at com.google.android.gms.internal.zzqa.onConnected(Unknown Source) at com.google.android.gms.internal.zzpp.onConnected(Unknown Source) at com.google.android.gms.common.internal.zzk$1.onConnected(Unknown Source) at com.google.android.gms.common.internal.zzd$zzj.zzasd(Unknown Source) at com.google.android.gms.common.internal.zzd$zza.zzc(Unknown Source) at com.google.android.gms.common.internal.zzd$zza.zzv(Unknown Source) at com.google.android.gms.common.internal.zzd$zze.zzasf(Unknown Source) at com.google.android.gms.common.internal.zzd$zzd.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5938) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

最佳答案

setContentView() 方法下面定义 textView

像这样...

setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text_view);

关于android - Erron 在 Android 中使用 GoogleApiClient 获取最后已知位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39264600/

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