【发布时间】:2016-10-24 18:41:51
【问题描述】:
我遇到了问题。运行 app 后,我设置的 textview 没有显示出来。这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView rangeLabel = (TextView) findViewById(R.id.range_label);
final TextView beaconMacAddressLabel = (TextView) findViewById(R.id.beacon_mac_address_label);
final TextView beaconUuidLabel = (TextView) findViewById(R.id.beacon_uuid_label);
final TextView beaconVersionLabel = (TextView) findViewById(R.id.beacon_major_minor_label);
final TextView beaconStatsLabel = (TextView) findViewById(R.id.beacon_stats_label);
rangeLabel.setTextColor(Color.BLACK);
rangeLabel.setText(R.string.question_mark);
region = new Region("regionid", null, null, null);
beaconManager = new BeaconManager(this);
beaconManager.setRangingListener(new RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!beacons.isEmpty()) {
Beacon closestBeacon = beacons.get(0);
beaconMacAddressLabel.setText(getString(R.string.mac_address) + " : " + closestBeacon.getMacAddress());
beaconUuidLabel.setText(closestBeacon.getProximityUUID());
beaconVersionLabel.setText(getString(R.string.major) + ": "+closestBeacon.getMajor() +
" " + getString(R.string.minor) + ": " + closestBeacon.getMinor());
beaconStatsLabel.setText(getString(R.string.power) + ": " +
closestBeacon.getMeasuredPower() +
" " +
getString(R.string.dbm) +
" | " +
getString(R.string.rssi) +
": " + closestBeacon.getRssi());
double accuracy = Utils.computeAccuracy(closestBeacon);
switch (Utils.proximityFromAccuracy(accuracy)) {
case FAR:
rangeLabel.setText(R.string.cold);
rangeLabel.setTextColor(getResources().getColor(R.color.cold_colour));
rangeLabel.setBackgroundResource(R.drawable.cold_indicator_background);
break;
case NEAR:
rangeLabel.setText(R.string.warm);
rangeLabel.setTextColor(getResources().getColor(R.color.warm_colour));
rangeLabel.setBackgroundResource(R.drawable.warm_indicator_background);
break;
case IMMEDIATE:
rangeLabel.setText(R.string.hot);
rangeLabel.setTextColor(getResources().getColor(R.color.hot_colour));
rangeLabel.setBackgroundResource(R.drawable.hot_indicator_background);
break;
case UNKNOWN:
rangeLabel.setText(R.string.question_mark);
rangeLabel.setTextColor(Color.BLACK);
rangeLabel.setBackgroundResource(R.drawable.indicator_background);
break;
我的代码有什么问题吗?仅显示标题(在 XML 中设置)和范围(在第 10 行设置)。其他都没有。有谁知道为什么? 我试图改变颜色,但没有帮助。猜猜我的代码有问题
如果需要,这是我的 XML:
【问题讨论】:
-
您是否尝试记录 beacons.size() 以防不满足 !beacons.empty() 条件?