github上有一个项目https://github.com/codeka/advbatterygraph它在小部件中显示了有关电池的不同属性,例如瞬时电流(mA/h)等。我下载了该项目,编译了它,在我的 Nexus 5 上运行它,它运行得很好。
我想做的就是从此小部件获取此即时电流 (mA) 值,并使用 TextView 将其显示在 Activity 中。
我研究了代码来弄清楚问题,我发现:
1. 该值存储在 BatteryGraphWidgetProvider 中的 String(curr) 中类。
2. BatteryStatus类有一个方法getBatteryCurrentInstant();
我在我的 Activity 中尝试了类似的操作,但不够幸运:
BatteryStatus b = new BatteryStatus();
b.getBatteryCurrentInstant;
/*Made a TextView Variable, and by using "findViewById" method I got the
reference to that variable. Afterwards, I set it like this:*/
mMy_Variable.setText( (int) b.getBatteryCurrentInstant() );
结果: “不幸的是,应用程序停止工作”。
请原谅我,如果这是一个太多的新手问题,但我刚刚开始进行 Android 开发,我很确定每个人都经历过这个阶段。任何有关我如何完成这项任务的帮助/想法/建议将不胜感激!
最佳答案
android TextView 有多种 setText
实现。接受整数值的,尝试从您的资源中检索字符串。
read more here 。您应该将您的值作为 String
传递。您可以使用Integer.toString(int)
。
关于java - 无法在 Activity 中将小部件中的值显示为 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45526978/