在Android中使用googleapicclient获取最后已知位置的错误



试图获取设备的最后位置

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。分配任务,PID: 24018java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'mzkhan.assignmenttask.MainActivity.onConnected (MainActivity.java: 70)(来源不明)(来源不明)(来源不明)(来源不明)(来源不明)(来源不明)$1.onConnected(来源不明)$zzj.zzasd(来源不明)$zza.zzc(来源不明)$zza.zzv(来源不明)$zze.zzasf(来源不明)$zzd.handleMessage(来源未知)android.os.Handler.dispatchMessage (Handler.java: 102)android.os.Looper.loop (Looper.java: 145)android.app.ActivityThread.main (ActivityThread.java: 5938)在java.lang.reflect.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)

setContentView()方法下面定义textView

这样的…

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

请查看下面的代码更改。在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();
    }
}

相关内容

  • 没有找到相关文章

最新更新