我想将我的 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?)
}
}