我的Handler
中有一个MainActivity
。此Handler
必须更新Fragment
布局中MainActivity
之一的用户界面。
然后我有一个单独的类,它有一个循环;当我创建这个类时,让我们调用它myLooper
它会进行某种计算,然后将循环内部的信息发送到Handler
中的MainActivity
。然后,这会将该数据更新为我在GraphView
的片段中实现的MainActivity
。
FLOW:
第1步:计算MyLooper
上的数据。
第2步:通过我的MainActivity
将数据发送到Handler
。
第3步:让我的Handler
更新MainActivity
Fragment
个GraphView
包含已发送的数据。
相关代码:
public class MainActivity extends ActionBarActivity implements
ActionBar.TabListener {
public MyLooper myDataLooper;
Fragment graphFrag = fragment_graphpage.newInstance(1);
public final Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
((fragment_graphpage) graphFrag).updateGraphUI(msg.getData().getFloat("myvoltage"));
}
};
SectionsPagerAdapter mSectionsPagerAdapter;
globalControlInstance globalControl;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
globalControl = globalControlInstance.getInstance();
myDataLooper = (IOIOBoard) getApplicationContext();
myDataLooper.create(getBaseContext(), this,_myIOIOHandler);
myDataLooper.start();
}
MyLooper课程:
public class MyLooper extends Application implements IOIOLooperProvider {
globalControlInstance globalData = globalControlInstance.getInstance();
public AnalogInput AI1;
public Handler IOIOHandler;
private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
this, this);
protected void create(Context myContext, Activity myActivity, Handler myHandler) {
IOIOHandler = myHandler;
helper_.create();
}
protected void destroy() {
helper_.destroy();
}
protected void start() {
helper_.start();
}
protected void stop() {
helper_.stop();
}
protected void restart() {
helper_.restart();
}
class Looper extends BaseIOIOLooper {
@Override
protected void setup() throws ConnectionLostException {
}
@Override
public void loop() throws ConnectionLostException, InterruptedException {
Message myVoltageMessage = new Message();
Bundle myVoltageBundle = new Bundle();
myVoltageBundle.putFloat("myvoltage", AI1.getVoltage());
myVoltageMessage.setData(myVoltageBundle);
IOIOHandler.sendMessage(myVoltageMessage);
}
}
@Override
public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
return new Looper();
}
}
最后是包含Fragment
GraphView
方法的updateGraphUI
。:
public void updateGraphUI(float newReading) {
final double newReadingDouble = newReading;
mTimer2 = new Runnable() {
@Override
public void run() {
graphLoopCounter++;
if (globalData.isLiveUpdatingEnabled()) {
alarmCheck(newReadingDouble);
globalData.setGraph2LastXValue(globalData
.getGraph2LastXValue() + 1d);
globalData.getGraphViewSeries().appendData(
new GraphViewData(globalData.getGraph2LastXValue(),
newValue), true, 1000);
if (globalData.getGraphPeak() < newReadingDouble) {
globalData.setGraphPeak(newReadingDouble);
graphPeak.setText("Graph Peak: "
+ Double.toString(newReadingDouble));
}
avgSum += globalData.getLatestGraphData();
graphAvg.setText("Graph Avg: " + Double.toString(avgSum/graphLoopCounter));
mHandler.postDelayed(this, 100);
}
}
};
mHandler.postDelayed(mTimer2, 100);
}
其中mHandler
是我Fragment
实现中的处理程序。 mTimer1
和mTimer2
都是Runnable
s。
我的问题是;无论我尝试做什么,数据都不会发送到我的GraphView,它永远不会更新......
答案 0 :(得分:1)
在MainActivity中,您将Handler实例命名为“myHandler”,但随后将MyLooper类命名为“_myIOIOHandler”。