I have been trying to integrate my very simple voice recorder app with the ability to get the current user's location and display it on the screen. I want it to start searching for the location as soon as the app starts but I have logic problems with my code. I am still learning but I have read through a lot of the documentation and many different tutorials/sample code but nothing seems to work. I think I am very close to my solution, my voice recorder app works however it's not coming along well with getting user's location... can you guys help me? Any help is greatly appreciated!
Here is my code (note it may seem a little long but it's only due to extra spacing):
public class AndroidPOIActivity extends Activity implements LocationListener
{
EditText textBoxMessage = null;
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
/***************** Record Button ********************/
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording()
{
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
/***************** Play Button ********************/
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
/***************** File Saver ********************/
public AndroidPOIActivity() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
/***************** On Create ********************/
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
//preparing linear layout
LinearLayout ll = new LinearLayout(this);
//record button
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
//play button
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
TextView locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
ll.addView(locationText,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
//getSystemService(LOCATION_SERVICE);
//setting linear layout
setContentView(ll);
//getting a reference to the system location manager
LocationManager locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
Log.d(LOG_TAG, location.toString());
this.onLocationChanged(location);
}
//defining listener that responds to location updates
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
@Override
public void onProviderDisabled(String arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location)
{
makeUseOfNewLocation(location);
}
private void makeUseOfNewLocation(Location location)
{
double lon = (double) (location.getLongitude() * 1E6);
double lat = (double) (location.getLatitude() * 1E6);
int lontitude = (int)lon;
int latitude = (int)lat;
Toast.makeText(getApplicationContext(), "New Lontitue = "+ lontitude +"\n New Latitute = "+ latitude, Toast.LENGTH_LONG).show();
}
}
My code has permissions as well in manifest:
Here is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/lblLocationInfo"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Waiting for location..."/>
<Button android:id="@+id/mRecordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Recording"
android:layout_gravity="center"></Button>
<Button android:id="@+id/mPlayButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Playing"
android:layout_gravity="center"></Button>
</LinearLayout>
Stacktrace: [2011-11-15 14:51:47 - AndroidPOI] ------------------------------
[2011-11-15 14:51:47 - AndroidPOI] Android Launch!
[2011-11-15 14:51:47 - AndroidPOI] adb is running normally.
[2011-11-15 14:51:47 - AndroidPOI] Performing org.me.androidpoi.AndroidPOIActivity activity launch
[2011-11-15 14:51:47 - AndroidPOI] Automatic Target Mode: launching new emulator with compatible AVD 'android_2-2'
[2011-11-15 14:51:47 - AndroidPOI] Launching a new emulator with Virtual Device 'android_2-2'
[2011-11-15 14:52:17 - Emulator] emulator: emulator window was out of view and was recentred
[2011-11-15 14:52:17 - Emulator]
[2011-11-15 14:52:36 - AndroidPOI] New emulator found: emulator-5554
[2011-11-15 14:52:36 - AndroidPOI] Waiting for HOME ('android.process.acore') to be launched...
[2011-11-15 14:53:51 - AndroidPOI] HOME is up on device 'emulator-5554'
[2011-11-15 14:53:51 - AndroidPOI] Uploading AndroidPOI.apk onto device 'emulator-5554'
[2011-11-15 14:53:51 - AndroidPOI] Installing AndroidPOI.apk...
[2011-11-15 14:54:27 - AndroidPOI] Success!
[2011-11-15 14:54:27 - AndroidPOI] Starting activity org.me.androidpoi.AndroidPOIActivity on device emulator-5554
[2011-11-15 14:54:29 - AndroidPOI] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=org.me.androidpoi/.AndroidPOIActivity }
LogCat:
11-15 14:53:19.335: ERROR/Zygote(32): setreuid() failed. errno: 2
11-15 14:53:35.093: ERROR/Zygote(32): setreuid() failed. errno: 17
11-15 14:53:37.553: ERROR/BatteryService(67): usbOnlinePath not found
11-15 14:53:37.553: ERROR/BatteryService(67): batteryVoltagePath not found
11-15 14:53:37.553: ERROR/BatteryService(67): batteryTemperaturePath not found
11-15 14:53:37.603: ERROR/SurfaceFlinger(67): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
11-15 14:53:47.964: ERROR/EventHub(67): could not get driver version for /dev/input/mouse0, Not a typewriter
11-15 14:53:47.964: ERROR/EventHub(67): could not get driver version for /dev/input/mice, Not a typewriter
11-15 14:53:48.414: ERROR/System(67): Failure starting core service
11-15 14:53:48.414: ERROR/System(67): java.lang.SecurityException
11-15 14:53:48.414: ERROR/System(67): at android.os.BinderProxy.transact(Native Method)
11-15 14:53:48.414: ERROR/System(67): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
11-15 14:53:48.414: ERROR/System(67): at android.os.ServiceManager.addService(ServiceManager.java:72)
11-15 14:53:48.414: ERROR/System(67): at com.android.server.ServerThread.run(SystemServer.java:184)
11-15 14:53:49.825: ERROR/SoundPool(67): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 14:53:49.845: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressStandard.ogg
11-15 14:53:49.845: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-15 14:53:49.857: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressDelete.ogg
11-15 14:53:49.857: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressReturn.ogg
11-15 14:53:52.643: ERROR/ThrottleService(67): Could not open GPS configuration file /etc/gps.conf
11-15 14:53:54.635: ERROR/logwrapper(149): executing /system/bin/tc failed: No such file or directory
11-15 14:53:54.786: ERROR/logwrapper(150): executing /system/bin/tc failed: No such file or directory
11-15 14:53:54.903: ERROR/logwrapper(151): executing /system/bin/tc failed: No such file or directory
11-15 14:54:10.153: ERROR/HierarchicalStateMachine(67): TetherMaster - unhandledMessage: msg.what=3
11-15 14:54:30.744: ERROR/AndroidRuntime(279): FATAL EXCEPTION: main
11-15 14:54:30.744: ERROR/AndroidRuntime(279): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.me.androidpoi/org.me.androidpoi.AndroidPOIActivity}: java.lang.NullPointerException
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): Caused by: java.lang.NullPointerException
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.view.ViewGroup.addViewInner(ViewGroup.java:1969)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.view.ViewGroup.addView(ViewGroup.java:1865)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.view.ViewGroup.addView(ViewGroup.java:1845)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at org.me.androidpoi.AndroidPOIActivity.onCreate(AndroidPOIActivity.java:169)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-15 14:54:30.744: ERROR/AndroidRuntime(279): ... 11 more
You're getting a null pointer exception:
1-15 14:54:30.744: ERROR/AndroidRuntime(279): at org.me.androidpoi.AndroidPOIActivity.onCreate(AndroidPOIActivity.java:169)
I don't know where line 169 is in your code, but check that out and make sure everything is kosher there.