Sign in with
Sign up | Sign in
Your question

Accessing data from other class

Last response: in Applications
Share
December 23, 2012 10:13:05 PM

Hello I am having trouble accessing the acceleration data from an accelerometer class I made.
Here is the code I am using. i have an accelerometer class in a seperate file which does all the accelerometer stuff and I have another class that uses the acceleration data.

There are no errors. the app just crashes.
I have found the cause of the crash to be when the line acc.create(); is run.
I am sure I made a mistake in the way I am trying to get data from the class but Im not sure how to go about doing it another way.

Any ideas?

Main.java
  1. package com.example.bounce;
  2.  
  3. import android.hardware.SensorManager;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.view.Menu;
  8.  
  9. public class Main extends Activity {
  10. Accelerometer acc;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. acc.create();
  16.  
  17. acc.mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  18. }
  19.  
  20. @Override
  21. public boolean onCreateOptionsMenu(Menu menu) {
  22. // Inflate the menu; this adds items to the action bar if it is present.
  23. getMenuInflater().inflate(R.menu.activity_main, menu);
  24.  
  25.  
  26. return true;
  27. }
  28.  
  29. @Override
  30. protected void onResume() {
  31. super.onResume();
  32. // acc.onResume();
  33. }
  34.  
  35. @Override
  36. protected void onPause() {
  37. super.onPause();
  38. // acc.onPause();
  39. }
  40.  
  41.  
  42. }


acceleromter.java
  1. package com.example.bounce;
  2.  
  3. import android.content.Context;
  4. import android.hardware.Sensor;
  5. import android.hardware.SensorEvent;
  6. import android.hardware.SensorEventListener;
  7. import android.hardware.SensorManager;
  8.  
  9. public class Accelerometer implements SensorEventListener{
  10.  
  11. public SensorManager mSensorManager;
  12. public Sensor mAccelerometer;
  13.  
  14. public float[] acceleration;
  15.  
  16. public void create() {
  17.  
  18. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  19. }
  20.  
  21. @Override
  22. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  23. // TODO Auto-generated method stub
  24.  
  25. }
  26.  
  27. @Override
  28. public void onSensorChanged(SensorEvent event) {
  29. acceleration[0] = event.values[0];
  30. acceleration[1] = event.values[1];
  31. acceleration[2] = event.values[2];
  32. }
  33.  
  34. protected void onResume() {
  35.  
  36. mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  37. }
  38.  
  39. protected void onPause() {
  40.  
  41. mSensorManager.unregisterListener(this);
  42. }
  43.  
  44.  
  45. }

More about : accessing data class

December 24, 2012 4:26:16 AM

Instead Of:

  1. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);


Try:

  1. try
  2. {
  3. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  4. }
  5. catch (Exception ex)
  6. {
  7. ex.printStackTrace();
  8. }
December 24, 2012 7:52:56 AM

thanks for the reply. The code still crashes:
  1. 12-24 09:51:08.920: D/ActivityThread(22606): <<< done: 110
  2. 12-24 09:51:08.920: E/ActivityThread(22606): >>> handling: 100
  3. 12-24 09:51:08.980: D/AndroidRuntime(22606): Shutting down VM
  4. 12-24 09:51:08.980: W/dalvikvm(22606): threadid=1: thread exiting with uncaught exception (group=0x2aac4560)
  5. 12-24 09:51:08.990: E/AndroidRuntime(22606): FATAL EXCEPTION: main
  6. 12-24 09:51:08.990: E/AndroidRuntime(22606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bounce/com.example.bounce.Main}: java.lang.NullPointerException
  7. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1745)
  8. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1761)
  9. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
  10. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
  11. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.os.Handler.dispatchMessage(Handler.java:99)
  12. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.os.Looper.loop(Looper.java:130)
  13. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.main(ActivityThread.java:3822)
  14. 12-24 09:51:08.990: E/AndroidRuntime(22606): at java.lang.reflect.Method.invokeNative(Native Method)
  15. 12-24 09:51:08.990: E/AndroidRuntime(22606): at java.lang.reflect.Method.invoke(Method.java:507)
  16. 12-24 09:51:08.990: E/AndroidRuntime(22606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  17. 12-24 09:51:08.990: E/AndroidRuntime(22606): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  18. 12-24 09:51:08.990: E/AndroidRuntime(22606): at dalvik.system.NativeStart.main(Native Method)
  19. 12-24 09:51:08.990: E/AndroidRuntime(22606): Caused by: java.lang.NullPointerException
  20. 12-24 09:51:08.990: E/AndroidRuntime(22606): at com.example.bounce.Main.onCreate(Main.java:15)
  21. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  22. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
  23. 12-24 09:51:08.990: E/AndroidRuntime(22606): ... 11 more
Related resources
December 24, 2012 8:00:07 AM

gordon13 said:
thanks for the reply. The code still crashes:
  1. 12-24 09:51:08.920: D/ActivityThread(22606): <<< done: 110
  2. 12-24 09:51:08.920: E/ActivityThread(22606): >>> handling: 100
  3. 12-24 09:51:08.980: D/AndroidRuntime(22606): Shutting down VM
  4. 12-24 09:51:08.980: W/dalvikvm(22606): threadid=1: thread exiting with uncaught exception (group=0x2aac4560)
  5. 12-24 09:51:08.990: E/AndroidRuntime(22606): FATAL EXCEPTION: main
  6. 12-24 09:51:08.990: E/AndroidRuntime(22606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bounce/com.example.bounce.Main}: java.lang.NullPointerException
  7. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1745)
  8. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1761)
  9. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
  10. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
  11. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.os.Handler.dispatchMessage(Handler.java:99)
  12. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.os.Looper.loop(Looper.java:130)
  13. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.main(ActivityThread.java:3822)
  14. 12-24 09:51:08.990: E/AndroidRuntime(22606): at java.lang.reflect.Method.invokeNative(Native Method)
  15. 12-24 09:51:08.990: E/AndroidRuntime(22606): at java.lang.reflect.Method.invoke(Method.java:507)
  16. 12-24 09:51:08.990: E/AndroidRuntime(22606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  17. 12-24 09:51:08.990: E/AndroidRuntime(22606): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  18. 12-24 09:51:08.990: E/AndroidRuntime(22606): at dalvik.system.NativeStart.main(Native Method)
  19. 12-24 09:51:08.990: E/AndroidRuntime(22606): Caused by: java.lang.NullPointerException
  20. 12-24 09:51:08.990: E/AndroidRuntime(22606): at com.example.bounce.Main.onCreate(Main.java:15)
  21. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  22. 12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
  23. 12-24 09:51:08.990: E/AndroidRuntime(22606): ... 11 more

The exception is still there, but it was not caught. Try to check for exceptions when you initialize the object and see what it says.
December 24, 2012 8:12:48 AM

Ive tried checking for exceptions where the acc.create() method is called and now the app doesn't crash but I still get the errors.

  1. 12-24 10:08:27.820: W/System.err(23020): java.lang.NullPointerException
  2. 12-24 10:08:27.870: W/System.err(23020): at com.example.bounce.Main.onCreate(Main.java:21)
  3. 12-24 10:08:27.870: W/System.err(23020): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  4. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
  5. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1761)
  6. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
  7. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
  8. 12-24 10:08:27.870: W/System.err(23020): at android.os.Handler.dispatchMessage(Handler.java:99)
  9. 12-24 10:08:27.870: W/System.err(23020): at android.os.Looper.loop(Looper.java:130)
  10. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.main(ActivityThread.java:3822)
  11. 12-24 10:08:27.870: W/System.err(23020): at java.lang.reflect.Method.invokeNative(Native Method)
  12. 12-24 10:08:27.870: W/System.err(23020): at java.lang.reflect.Method.invoke(Method.java:507)
  13. 12-24 10:08:27.870: W/System.err(23020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  14. 12-24 10:08:27.870: W/System.err(23020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  15. 12-24 10:08:27.870: W/System.err(23020): at dalvik.system.NativeStart.main(Native Method)
December 24, 2012 8:25:15 AM

gordon13 said:
Ive tried checking for exceptions where the acc.create() method is called and now the app doesn't crash but I still get the errors.

  1. 12-24 10:08:27.820: W/System.err(23020): java.lang.NullPointerException
  2. 12-24 10:08:27.870: W/System.err(23020): at com.example.bounce.Main.onCreate(Main.java:21)
  3. 12-24 10:08:27.870: W/System.err(23020): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  4. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
  5. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1761)
  6. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
  7. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
  8. 12-24 10:08:27.870: W/System.err(23020): at android.os.Handler.dispatchMessage(Handler.java:99)
  9. 12-24 10:08:27.870: W/System.err(23020): at android.os.Looper.loop(Looper.java:130)
  10. 12-24 10:08:27.870: W/System.err(23020): at android.app.ActivityThread.main(ActivityThread.java:3822)
  11. 12-24 10:08:27.870: W/System.err(23020): at java.lang.reflect.Method.invokeNative(Native Method)
  12. 12-24 10:08:27.870: W/System.err(23020): at java.lang.reflect.Method.invoke(Method.java:507)
  13. 12-24 10:08:27.870: W/System.err(23020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  14. 12-24 10:08:27.870: W/System.err(23020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  15. 12-24 10:08:27.870: W/System.err(23020): at dalvik.system.NativeStart.main(Native Method)


It's saying something is null. Are you sure it does not call any methods before the object is created. Try making it a final object.
December 24, 2012 10:38:43 AM

I looked around and found a different way of doing this but I have a feeling its doing the same thing Im doing but in a different way. The app still crashes with same error. What do you mean by object? The accelerometer?


  1. public class Main extends Activity{
  2.  
  3. Accelerometer acc;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. acc = new Accelerometer(this);
  11.  
  12. float test = acc.acceleration[0];
  13.  
  14. }


  1. public class Accelerometer implements SensorEventListener{
  2.  
  3. public SensorManager mSensorManager = null;
  4. public Sensor mAccelerometer;
  5. Activity A;
  6.  
  7. public float[] acceleration;
  8.  
  9. public Accelerometer(Activity A) {
  10. this.A = A;
  11. mSensorManager = (SensorManager)this.A.getSystemService(Context.SENSOR_SERVICE);
  12. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  13. mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  14.  
  15.  
  16. }
December 24, 2012 12:24:12 PM

gordon13 said:
I looked around and found a different way of doing this but I have a feeling its doing the same thing Im doing but in a different way. The app still crashes with same error. What do you mean by object? The accelerometer?


  1. public class Main extends Activity{
  2.  
  3. Accelerometer acc;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. acc = new Accelerometer(this);
  11.  
  12. float test = acc.acceleration[0];
  13.  
  14. }


  1. public class Accelerometer implements SensorEventListener{
  2.  
  3. public SensorManager mSensorManager = null;
  4. public Sensor mAccelerometer;
  5. Activity A;
  6.  
  7. public float[] acceleration;
  8.  
  9. public Accelerometer(Activity A) {
  10. this.A = A;
  11. mSensorManager = (SensorManager)this.A.getSystemService(Context.SENSOR_SERVICE);
  12. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  13. mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  14.  
  15.  
  16. }

What IDE and Lib are you using? I may be able to debug it myself.
December 24, 2012 12:38:33 PM

Im using Eclipse with the ADT plugin. It all came in a single package from the Android website so I assume its all standard stuff. I havent added anything myself
January 19, 2013 2:43:51 PM

You're trying to debug an Android app on the PC with no prior experience with Java by the looks of it.

Do you already know how to create an object in Java?
!