Android做GPS定位时,出现button.setOnClickListener老是出现NUllPointerException。代码如下: 10

代码:publicclassLocationActivity1extendsActivity{@OverridepublicvoidonCreate(Bundlesave... 代码:public class LocationActivity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new MyButtonListener());
}

class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
LocationManager lm = (LocationManager)LocationActivity1.this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener());
}
}

class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
System.out.println(location.getLongitude());
System.out.println(location.getLatitude()); }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
public void onProviderEnabled(String provider) { } @Override
public void onProviderDisabled(String provider) {}
}
}
错误:11-04 14:16:58.170: E/AndroidRuntime(1024): java.lang.RuntimeException: Unable to start activity ComponentInfo{hui.location/hui.location.LocationActivity1}: java.lang.NullPointerException
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.os.Looper.loop(Looper.java:123)
11-04 14:16:58.170: E/AndroidRuntime(1024): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 14:16:58.170: E/AndroidRuntime(1024): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 14:16:58.170: E/AndroidRuntime(1024): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 14:16:58.170: E/AndroidRuntime(1024): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 14:16:58.170: E/AndroidRuntime(1024): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 14:16:58.170: E/AndroidRuntime(1024): at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at hui.location.LocationActivity1.onCreate(LocationActivity1.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
展开
 我来答
klggg小店
2011-11-08 · 超过11用户采纳过TA的回答
知道答主
回答量:31
采纳率:0%
帮助的人:22万
展开全部
一般是调用的对象没初始化
绑定 click事件可以用
setOnClickListener(this);
类这样声明
xxx extends Activity implements OnClickListener{
余肚厕
2011-11-05
知道答主
回答量:4
采纳率:0%
帮助的人:6667
展开全部
之前我也有遇到过,没有实质解决。改用 getLastKnownLocation(LocationManager.NETWORK_PROVIDER) 可以得到位置 希望能帮到您vaqytqfniu
追问
谢谢哈!我后来又重新建了一个,就可以了。不知道为什么

参考资料: 最宝网安卓论坛

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式