Accessing data from other class

gordon13

Distinguished
Sep 8, 2011
5
0
18,510
0
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
[cpp]package com.example.bounce;

import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;

public class Main extends Activity {
Accelerometer acc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc.create();

acc.mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);


return true;
}

@Override
protected void onResume() {
super.onResume();
// acc.onResume();
}

@Override
protected void onPause() {
super.onPause();
// acc.onPause();
}


}
[/cpp]

acceleromter.java
[cpp]package com.example.bounce;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class Accelerometer implements SensorEventListener{

public SensorManager mSensorManager;
public Sensor mAccelerometer;

public float[] acceleration;

public void create() {

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

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
acceleration[0] = event.values[0];
acceleration[1] = event.values[1];
acceleration[2] = event.values[2];
}

protected void onResume() {

mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {

mSensorManager.unregisterListener(this);
}


}
[/cpp]
 

melikepie

Distinguished
Dec 14, 2011
17
0
18,560
0
Instead Of:

[cpp]mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);[/cpp]

Try:

[cpp]try
{
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
catch (Exception ex)
{
ex.printStackTrace();
}[/cpp]
 

gordon13

Distinguished
Sep 8, 2011
5
0
18,510
0
thanks for the reply. The code still crashes:
[cpp]12-24 09:51:08.920: D/ActivityThread(22606): <<< done: 110
12-24 09:51:08.920: E/ActivityThread(22606): >>> handling: 100
12-24 09:51:08.980: D/AndroidRuntime(22606): Shutting down VM
12-24 09:51:08.980: W/dalvikvm(22606): threadid=1: thread exiting with uncaught exception (group=0x2aac4560)
12-24 09:51:08.990: E/AndroidRuntime(22606): FATAL EXCEPTION: main
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
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1745)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1761)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.os.Looper.loop(Looper.java:130)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.main(ActivityThread.java:3822)
12-24 09:51:08.990: E/AndroidRuntime(22606): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 09:51:08.990: E/AndroidRuntime(22606): at java.lang.reflect.Method.invoke(Method.java:507)
12-24 09:51:08.990: E/AndroidRuntime(22606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-24 09:51:08.990: E/AndroidRuntime(22606): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-24 09:51:08.990: E/AndroidRuntime(22606): at dalvik.system.NativeStart.main(Native Method)
12-24 09:51:08.990: E/AndroidRuntime(22606): Caused by: java.lang.NullPointerException
12-24 09:51:08.990: E/AndroidRuntime(22606): at com.example.bounce.Main.onCreate(Main.java:15)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-24 09:51:08.990: E/AndroidRuntime(22606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
12-24 09:51:08.990: E/AndroidRuntime(22606): ... 11 more
[/cpp]
 

melikepie

Distinguished
Dec 14, 2011
17
0
18,560
0

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.
 

gordon13

Distinguished
Sep 8, 2011
5
0
18,510
0
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.

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

melikepie

Distinguished
Dec 14, 2011
17
0
18,560
0


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.
 

gordon13

Distinguished
Sep 8, 2011
5
0
18,510
0
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?


[cpp]
public class Main extends Activity{

Accelerometer acc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

acc = new Accelerometer(this);

float test = acc.acceleration[0];

}
[/cpp]

[cpp]public class Accelerometer implements SensorEventListener{

public SensorManager mSensorManager = null;
public Sensor mAccelerometer;
Activity A;

public float[] acceleration;

public Accelerometer(Activity A) {
this.A = A;
mSensorManager = (SensorManager)this.A.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);


}[/cpp]
 

melikepie

Distinguished
Dec 14, 2011
17
0
18,560
0

What IDE and Lib are you using? I may be able to debug it myself.
 

gordon13

Distinguished
Sep 8, 2011
5
0
18,510
0
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
 

Scott_D_Bowen

Honorable
Nov 28, 2012
50
0
10,590
1
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?
 
Thread starter Similar threads Forum Replies Date
D Apps General Discussion 0
C Apps General Discussion 1
S Apps General Discussion 2
H Apps General Discussion 1
A Apps General Discussion 1
J Apps General Discussion 6
RainOfPain125 Apps General Discussion 5
D Apps General Discussion 2
B Apps General Discussion 1
M Apps General Discussion 3
W Apps General Discussion 1
K Apps General Discussion 1
C Apps General Discussion 2
C Apps General Discussion 1
G Apps General Discussion 8
K Apps General Discussion 2
gigaman Apps General Discussion 6
W Apps General Discussion 1
K Apps General Discussion 1
L Apps General Discussion 3

ASK THE COMMUNITY