Goal:
创建用于图形生成的辅助类
背景:
我有 3 个片段,每个片段收集一些传感器数据(加速度计、陀螺仪、旋转)并使用 GraphView 绘制图表。以下是其中一个片段的代码(该代码当前工作正常):
public class GyroscopeFragment extends Fragment implements SensorEventListener {
private final short TYPE_GYROSCOPE = Sensor.TYPE_GYROSCOPE;
private final short POLL_FREQUENCY = 100; //in milliseconds
private final short MAX_POINTS_DISPLAYED = 50;
private SensorManager sensorManager;
private Sensor sensor;
private Sensor gyroscope;
private long lastUpdate = -1;
float curGyroX;
float curGyroY;
float curGyroZ;
private LineGraphSeries<DataPoint> gyroXSeries;
private LineGraphSeries<DataPoint> gyroYSeries;
private LineGraphSeries<DataPoint> gyroZSeries;
private double graphXTime = 1d;
public GyroscopeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gyroscope, container, false);
//Set the nav drawer item highlight
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.navigationView.setCheckedItem(R.id.nav_gyroscope);
//Set actionbar title
mainActivity.setTitle("Gyroscope");
//Sensor manager
sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
gyroscope = (Sensor) sensorManager.getDefaultSensor(TYPE_GYROSCOPE);
return view;
}
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_FASTEST);
//Create graph
GraphView gyroGraph = (GraphView) getView().findViewById(R.id.gyroGraph);
gyroGraph.getLegendRenderer().setVisible(true);
gyroGraph.getLegendRenderer().setFixedPosition(0,0);
gyroGraph.getGridLabelRenderer().setHighlightZeroLines(false);
gyroXSeries = new LineGraphSeries<DataPoint>();
gyroYSeries = new LineGraphSeries<DataPoint>();
gyroZSeries = new LineGraphSeries<DataPoint>();
gyroXSeries.setTitle("X");
gyroYSeries.setTitle("Y");
gyroZSeries.setTitle("Z");
gyroXSeries.setColor(Color.RED);
gyroYSeries.setColor(Color.GREEN);
gyroZSeries.setColor(Color.BLUE);
gyroGraph.addSeries(gyroXSeries);
gyroGraph.addSeries(gyroYSeries);
gyroGraph.addSeries(gyroZSeries);
gyroGraph.getViewport().setYAxisBoundsManual(true);
gyroGraph.getViewport().setMinY(-10);
gyroGraph.getViewport().setMaxY(10);
gyroGraph.getViewport().setXAxisBoundsManual(true);
gyroGraph.getViewport().setMinX(0);
gyroGraph.getViewport().setMaxX(MAX_POINTS_DISPLAYED);
}
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onDestroy() {
super.onDestroy();
sensorManager.unregisterListener(this);
}
@Override
public void onLowMemory() {
super.onLowMemory();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
sensor = event.sensor;
curGyroX = event.values[0];
curGyroY = event.values[1];
curGyroZ = event.values[2];
long curTime = System.currentTimeMillis();
long diffTime = (curTime - lastUpdate);
// only allow one update every POLL_FREQUENCY.
if (diffTime > POLL_FREQUENCY) {
lastUpdate = curTime;
graphXTime += 1d;
gyroXSeries.appendData(new DataPoint(graphXTime, curGyroX), true, MAX_POINTS_DISPLAYED);
gyroYSeries.appendData(new DataPoint(graphXTime, curGyroY), true, MAX_POINTS_DISPLAYED);
gyroZSeries.appendData(new DataPoint(graphXTime, curGyroZ), true, MAX_POINTS_DISPLAYED);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//Safe not to implement
}
}
我发现在这 3 个片段中我重复了很多图形生成代码onResume
所以我想创建一个 GraphHelper 类,我可以实例化它来创建图形,其优点是如果我想改变图形的外观,只需在一处更改代码。然而,我对 Java 很陌生,因此在创建这个新类时遇到了困难。
尝试的解决方案:
我设法提出以下课程来尝试解决该问题:
public class GraphHelper {
private final GraphView graph;
private final LineGraphSeries<DataPoint> xSeries;
private final LineGraphSeries<DataPoint> ySeries;
private final LineGraphSeries<DataPoint> zSeries;
public GraphHelper(GraphView graph, LineGraphSeries<DataPoint> xSeries,
LineGraphSeries<DataPoint> ySeries, LineGraphSeries<DataPoint> zSeries, short maxDisplayed){
this.graph = graph;
this.xSeries = xSeries;
this.ySeries = ySeries;
this.zSeries = zSeries;
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setFixedPosition(0, 0);
graph.getGridLabelRenderer().setHighlightZeroLines(false);
xSeries = new LineGraphSeries<DataPoint>();
ySeries = new LineGraphSeries<DataPoint>();
zSeries = new LineGraphSeries<DataPoint>();
xSeries.setTitle("X");
ySeries.setTitle("Y");
zSeries.setTitle("Z");
xSeries.setColor(Color.RED);
ySeries.setColor(Color.GREEN);
zSeries.setColor(Color.BLUE);
graph.addSeries(xSeries);
graph.addSeries(ySeries);
graph.addSeries(zSeries);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(-10);
graph.getViewport().setMaxY(10);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(maxDisplayed);
}
}
并改变了我的片段onResume
看起来像这样(其他一切都一样):
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_FASTEST);
//Create graph
GraphView gyroGraph = (GraphView) getView().findViewById(R.id.gyroGraph);
new GraphHelper(gyroGraph, gyroXSeries, gyroYSeries, gyroZSeries, MAX_POINTS_DISPLAYED);
}
问题#1:
每当我尝试查看图形片段时,都会收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.jjoe64.graphview.series.LineGraphSeries.appendData(com.jjoe64.graphview.series.DataPointInterface, boolean, int)' on a null object reference
这是指onSensorChanged
我试图将数据附加到该系列的方法。似乎不知道什么gyroXSeries
是因为我将其添加到助手中的图表中。
问题#2:
目前我声明gyroXSeries
在主片段类以及辅助类的顶部,并创建一个新的LineGraphSeries
辅助构造函数中的对象。有没有办法减少这段代码的重复呢?
EDIT
为了响应下面的评论,我进行了以下更改,新的 GraphHelper 类如下所示:
public class GraphHelper {
private final GraphView graph;
private final LineGraphSeries<DataPoint> xSeries;
private final LineGraphSeries<DataPoint> ySeries;
private final LineGraphSeries<DataPoint> zSeries;
public GraphHelper(GraphView graph, LineGraphSeries<DataPoint> xSeries,
LineGraphSeries<DataPoint> ySeries, LineGraphSeries<DataPoint> zSeries, short maxDisplayed){
this.graph = graph;
this.xSeries = xSeries;
this.ySeries = ySeries;
this.zSeries = zSeries;
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setFixedPosition(0, 0);
graph.getGridLabelRenderer().setHighlightZeroLines(false);
xSeries.setTitle("X");
ySeries.setTitle("Y");
zSeries.setTitle("Z");
xSeries.setColor(Color.RED);
ySeries.setColor(Color.GREEN);
zSeries.setColor(Color.BLUE);
graph.addSeries(xSeries);
graph.addSeries(ySeries);
graph.addSeries(zSeries);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(-10);
graph.getViewport().setMaxY(10);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(maxDisplayed);
}
}
陀螺仪片段与原来相同,变化为onResume
我上面做的
这会导致以下错误:
致命异常:主要
进程:net.binarysea.sensorload,PID:25997
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.jjoe64.graphview.series.LineGraphSeries.setTitle(java.lang.String)”
在net.binarysea.sensorload.GraphHelper。(GraphHelper.java:30)
在net.binarysea.sensorload.GyrscopeFragment.onResume(GyrscopeFragment.java:73)
在 android.support.v4.app.Fragment.performResume(Fragment.java:2005)
在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
在 android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
在 android.os.Handler.handleCallback(Handler.java:739)
在 android.os.Handler.dispatchMessage(Handler.java:95)
在 android.os.Looper.loop(Looper.java:148)
在 android.app.ActivityThread.main(ActivityThread.java:5417)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)