我一直在努力将我非常简单的录音笔应用程序与获取当前用户位置并将其显示在屏幕上的功能集成在一起。我希望它在应用程序启动后立即开始搜索位置,但我的代码存在逻辑问题。我还在学习,但我已经阅读了很多文档和许多不同的教程/示例代码,但似乎没有任何工作。我认为我非常接近我的解决方案,我的录音机应用程序可以工作,但它不能很好地获取用户的位置...你能帮我吗?非常感谢任何帮助!
这是我的代码(注意它可能看起来有点长,但这只是因为额外的间距):
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();
}
}
我的代码在manifest中也有权限:
这是我的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>
堆栈跟踪: [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
答案 0 :(得分:1)
你得到一个空指针异常:
1-15 14:54:30.744: ERROR/AndroidRuntime(279): at org.me.androidpoi.AndroidPOIActivity.onCreate(AndroidPOIActivity.java:169)
我不知道您的代码中的第169行,但检查出来并确保其中的所有内容都是犹太人。
答案 1 :(得分:1)
public class AndroidPOIActivity extends Activity
{
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 = new TextView(this);
ll.addView(locationText,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
private 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) {}
};
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
LocationManager locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
locationManager.removeUpdates(locationListener);
}
@Override
protected void onResume() {
LocationManager locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
super.onResume();
}
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();
}
}