我正在开发一个应用程序,无论何时单击获取位置(gpsBtn),GPS都将启用,用户的位置将显示在TextView中。我得到java.lang.NullPointerException错误,应用程序意外停止。我该如何解决这个问题?
public class MainActivity extends AppCompatActivity {
public LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());
Button gpsBtn = (Button)findViewById(R.id.getLocation);
gpsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
public void showCurrentLocation() {
// TODO Auto-generated method stub
final TextView textView=(TextView)findViewById(R.id.textView);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
textView.setText(String.valueOf(location.getLatitude()));
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mehrdad.myapplication" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
答案 0 :(得分:1)
尝试.....将final TextView textView=(TextView)findViewById(R.id.textView);
移至onCreate()
方法。还要在try和catch块中添加textView.setText(String.valueOf(location.getLatitude()));
行。
try{
textView.setText(String.valueOf(location.getLatitude()));
}catch(Exception e){
// exception occurs......
}
完整代码
public class MainActivity extends AppCompatActivity {
public LocationManager lm;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());
textView = (TextView)findViewById(R.id.textView);
Button gpsBtn = (Button)findViewById(R.id.getLocation);
gpsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});
public void showCurrentLocation() {
// TODO Auto-generated method stub
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try{
textView.setText(String.valueOf(location.getLatitude()));
}catch(Exception e){
// exception occurs......
}
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}