我想将textView的值设置在块Awareness.SnapshotApi.getWeather
之外,但它无法正常工作。每次我在标记区域设置值程序崩溃。我也想知道,如何从其他课程/活动中拨打getWeather()
?
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Awareness";
private GoogleApiClient mGoogleApiClient;
private TextView tv;
public int[] con;
private String[] conditions = {"Unknown", "Clear", "Cloudy", "Foggy", "Hazy", "Icy","Rainy", "Snowy", "Stromy","Wind"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addApi(Awareness.API)
.build();
mGoogleApiClient.connect();
initSnapshots();
}
public void initSnapshots() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
tv.setText("Permission failed");
// 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;
}
Awareness.SnapshotApi.getWeather(mGoogleApiClient)
.setResultCallback(new ResultCallback<WeatherResult>() {
@Override
public void onResult(@NonNull WeatherResult weatherResult) {
if (!weatherResult.getStatus().isSuccess()) {
tv.setText("weather failed");
return;
}
Weather weather = weatherResult.getWeather();
con = weather.getConditions();
tv.setText(conditions[con[0]] + "");
}
});
//tv.setText(conditions[con[0]] + ""); (<---------Crushes if I declare here,Why?)
}
}