Erron使用Android中的GoogleApiClient获取最后一个已知位置

时间:2016-09-01 06:55:17

标签: android google-api geolocation

尝试获取设备的最后位置

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                                                                          过程:mzkhan.assignmenttask,PID:24018                                                                          java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在null对象引用上                                                                              at mzkhan.assignmenttask.MainActivity.onConnected(MainActivity.java:70)                                                                              在com.google.android.gms.common.internal.zzl.zzo(未知来源)                                                                              在com.google.android.gms.internal.zzpy.zzm(未知来源)                                                                              在com.google.android.gms.internal.zzpw.zzapp(未知来源)                                                                              在com.google.android.gms.internal.zzpw.onConnected(未知来源)                                                                              在com.google.android.gms.internal.zzqa.onConnected(未知来源)                                                                              在com.google.android.gms.internal.zzpp.onConnected(未知来源)                                                                              在com.google.android.gms.common.internal.zzk $ 1.onConnected(未知来源)                                                                              在com.google.android.gms.common.internal.zzd $ zzj.zzasd(未知来源)                                                                              在com.google.android.gms.common.internal.zzd $ zza.zzc(未知来源)                                                                              在com.google.android.gms.common.internal.zzd $ zza.zzv(未知来源)                                                                              在com.google.android.gms.common.internal.zzd $ zze.zzasf(未知来源)                                                                              在com.google.android.gms.common.internal.zzd $ zzd.handleMessage(未知来源)                                                                              在android.os.Handler.dispatchMessage(Handler.java:102)                                                                              在android.os.Looper.loop(Looper.java:145)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5938)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在java.lang.reflect.Method.invoke(Method.java:372)                                                                              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1400)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

2 个答案:

答案 0 :(得分:1)

textView方法下方定义setContentView()

像这样......

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

答案 1 :(得分:0)

请查看下面的代码更改。您在onCreate中设置上下文视图之前已启动textview,因此在访问文本视图时它将返回NPE。

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