Majikero Gallardo Majikero Gallardo - 5 months ago 96
Android Question

Android Realtime accelerometer graph AChartEngine

Hi I'M making realtime graph by plotting SensorManager xyz but I have error.

My graph is working in normal activity but I'm trying to place it on Fragment because I will use iton viewpager with tablayout here's my code.

Note:


  1. Sensor data have no problem

  2. Graph is working in my first project I just transfer it on new project



LineGraph Java:

public class LineGraph {

private TimeSeries dataset = new TimeSeries("");

private TimeSeries dataset2 = new TimeSeries("");
private TimeSeries dataset3 = new TimeSeries("");

private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

private XYSeriesRenderer renderer = new XYSeriesRenderer();
private XYSeriesRenderer renderer2 = new XYSeriesRenderer();

private XYSeriesRenderer renderer3 = new XYSeriesRenderer();

private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

private GraphicalView view;

public LineGraph() {

mRenderer.setZoomButtonsVisible(true);
mRenderer.setXLabels(0);
mRenderer.setYLabels(0);
mDataset.addSeries(dataset);

renderer.setColor(Color.RED);
renderer.setFillPoints(false);
renderer.setLineWidth(10f);

mRenderer.addSeriesRenderer(renderer);

mDataset.addSeries(dataset2);
renderer2.setColor(Color.GREEN);
renderer2.setFillPoints(false);
renderer2.setLineWidth(10f);


mRenderer.setMargins(new int[]{0,0,0,0});

mRenderer.addSeriesRenderer(renderer2);


mDataset.addSeries(dataset3);

renderer3.setColor(Color.BLUE);
renderer3.setFillPoints(false);
renderer3.setLineWidth(10f);


mRenderer.addSeriesRenderer(renderer3);

}

public GraphicalView getView(Context context) {

view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}

public void addNewPoints(Point p) {
dataset.add(p.getX(), p.getY());
dataset2.add(p.getX2(), p.getY2());

dataset3.add(p.getX3(), p.getY3());
}

}


Shake Fragment Java << fragment to use in main java

public class Shake extends Fragment implements SensorEventListener {

private Sensor mySensor;
private SensorManager sensorManager;

public static GraphicalView view;
private LineGraph line = new LineGraph();
private LinearLayout ll;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shake, container, false);

return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

sensorManager = (SensorManager) getActivity().getSystemService(getActivity().SENSOR_SERVICE);

mySensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

sensorManager.registerListener(this, mySensor, sensorManager.SENSOR_DELAY_NORMAL);
ll = (LinearLayout) getActivity().findViewById(R.id.accelerometer_chart);

view = line.getView(getActivity()); // Error here

ll.addView(view);

}

@Override
public void onStart() {
super.onStart();

}

@Override
public void onResume() {
super.onResume();
}

@Override
public void onSensorChanged(SensorEvent event) {

Toast.makeText(getActivity(), "Sensor Changed", Toast.LENGTH_SHORT).show();
Point p = MockData.getDataFromReceiver(1, event.values[0], 2, event.values[1], 3, event.values[2]);

line.addNewPoints(p);

view.repaint();

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}


I debug it and It's seem I have error in

view = line.getView(getActivity());

because with out this line, project is working but no view of graph.

getView method from LineGraph java

public GraphicalView getView(Context context) {

view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}


LOGCAT ERROR

06-23 03:51:37.835 26465-26465/seismic.squareup.com.ushake E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
at seismic.squareup.com.ushake.Shake.onCreate(Shake.java:49)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1951) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029) at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:926) at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:876) at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1175)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:15518)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15518) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15518)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

Answer

If you look on the Android Fragment lifecycle on the official documentation, you'll see that onCreate() is being called before 'onCreateView()' and 'onActivityCreated()'. This means that in onCreate() your fragment's view is not created yet, and a fragment itself is not attached to Actvity so far. And you can't use getActivity().

Move your initialization into onActivityCreated() method

Comments