Android GPS位置获取 为什么总是不能运行??!!!

yr_lu 2012-02-22 05:28:27
在网上找了各个版本的GPS定位,可是都不能运行!要么是Sorry,the application has stopped unexpectly.Please try agian.要么就是在DDMS send经纬度之后毫无反应……哭……
到底哪里出了问题?请各位大侠们指点!不胜感激!
下面是源码:
MainActivity.java


package com.se7en;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.locationButtonId);
button.setOnClickListener(new ButtonListener());
}

private class ButtonListener implements OnClickListener{

@Override
public void onClick(View v){
LocationManager locationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
}
}

private class TestLocationListener implements LocationListener{


@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
System.out.println(location.getLongitude());
System.out.println(location.getLatitude());
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

}
}

manifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.se7en"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Location1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
</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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/locationButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+string/绑定监听器"/>
</LinearLayout>
...全文
621 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsh19901 2013-03-26
  • 打赏
  • 举报
回复
现在还有问题么,如果有....
c1230v 2012-11-30
  • 打赏
  • 举报
回复
引用 11 楼 zsh19901 的回复:
或者更高版本也可能会有问题? 我刚开始也是那样,不过后来解决了,是设计模式的问题,要符合android的设计模式才好,版本越高对这个要求就越高, 我现在碰见的问题是在2.3.3真机上老是获取不到location,不知道是手机的问题还是.....?
这位先生你的趁机处理好了没?我也是真机上不行
yr_lu 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

或者更高版本也可能会有问题?
我刚开始也是那样,不过后来解决了,是设计模式的问题,要符合android的设计模式才好,版本越高对这个要求就越高,
我现在碰见的问题是在2.3.3真机上老是获取不到location,不知道是手机的问题还是.....?
[/Quote]

是在2.3.3上不能运行。但我在真机上(2.3.6)可以获得location~你看看是不是程序有问题……
zsh19901 2012-04-26
  • 打赏
  • 举报
回复
或者更高版本也可能会有问题?
我刚开始也是那样,不过后来解决了,是设计模式的问题,要符合android的设计模式才好,版本越高对这个要求就越高,
我现在碰见的问题是在2.3.3真机上老是获取不到location,不知道是手机的问题还是.....?
zsh19901 2012-04-26
  • 打赏
  • 举报
回复
2.3.3上面有问题是吧
yr_lu 2012-03-04
  • 打赏
  • 举报
回复
是版本问题,在2.2上就可以运行了,我也不知道为什么
yr_lu 2012-02-23
  • 打赏
  • 举报
回复
可是现在又有新问题了……location返回的值总是null,无论用GPS_PROVIDER还是NETWORK_PROVIDER都是null。
念茜 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yr_lu 的回复:]

the application has stopped unexpectly问题解决了,原来是manifest文件中的activity名写错了。。。
[/Quote]

拿来别人代码借鉴很容易犯这个错误
yr_lu 2012-02-23
  • 打赏
  • 举报
回复
the application has stopped unexpectly问题解决了,原来是manifest文件中的activity名写错了。。。
yr_lu 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lianling121 的回复:]

因为你的模拟器不支持GSP功能,并且是获取上一次得到的location,而上一次根本就没有,所以location 总是为空的,我想如果在真机上应该可以的。
[/Quote]

怎么知道模拟器不支持GPS功能?
yr_lu 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yyaong123456 的回复:]

在模拟器中,楼主应该要先在DDMS视图中发送经纬度,再绑定监听器(button.setOnClickListener(new ButtonListener())),然后才能获取经纬度值,不然返回的就是空值。。。。
[/Quote]

我已经在DDMS中发送经纬度了,可是还是无法定位,显示location=null。而且发送经纬度之后,模拟器就会死机重启。。。
yyaong123456 2012-02-23
  • 打赏
  • 举报
回复
在模拟器中,楼主应该要先在DDMS视图中发送经纬度,再绑定监听器(button.setOnClickListener(new ButtonListener())),然后才能获取经纬度值,不然返回的就是空值。。。。
lianling121 2012-02-23
  • 打赏
  • 举报
回复
因为你的模拟器不支持GSP功能,并且是获取上一次得到的location,而上一次根本就没有,所以location 总是为空的,我想如果在真机上应该可以的。
yr_lu 2012-02-22
  • 打赏
  • 举报
回复
求指点!!

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧