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) 展开
@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) 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询