gpt4 book ai didi

java - 如何将 getLatitude() 的值放入 textview

转载 作者:行者123 更新时间:2023-11-30 10:59:18 26 4
gpt4 key购买 nike

我正在开发一个应用程序,只要您单击获取位置 (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 list :

<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>

最佳答案

尝试.....将您的final TextView textView=(TextView)findViewById(R.id.textView); 移动到onCreate() 方法。还要在 try 和 catch block 中添加 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

}
}

关于java - 如何将 getLatitude() 的值放入 textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com