带对话框的Android应用程序崩溃
我在一个AsyncTask类中获取设备的GPS坐标(通过getLocation方法),但是,如果GPS被禁用,我会打开一个对话框,使用户可以切换到“设置”区域并打开或取消GPS。每次在用户按下其中一个按钮之前打开对话框警报时,应用程序都会崩溃。我怎样才能解决它带对话框的Android应用程序崩溃,android,dialog,gps,Android,Dialog,Gps,我在一个AsyncTask类中获取设备的GPS坐标(通过getLocation方法),但是,如果GPS被禁用,我会打开一个对话框,使用户可以切换到“设置”区域并打开或取消GPS。每次在用户按下其中一个按钮之前打开对话框警报时,应用程序都会崩溃。我怎样才能解决它 public class StarTask extends AsyncTask<Void,Void,ArrayList<Song>>{ final int k_ThreadSleepTime =
public class StarTask extends AsyncTask<Void,Void,ArrayList<Song>>{
final int k_ThreadSleepTime = 3000;
final int k_MaxThreadTries = 7;
double latitude = 0;
double longitude = 0;
GPSTracker gps;
TestMain client;
@Override
protected void onPreExecute() {
super.onPreExecute();
gps = new GPSTracker(getApplication());
}
@Override
protected ArrayList<Song> doInBackground(Void... params) {
ArrayList<Song> list = new ArrayList();
client = new TestMain();
int tries = 0;
String o;
getLocation();
String url = builtURL();
try {
String jsonPageStr = client.doGetRequest(url);
JSONObject obj = new JSONObject(jsonPageStr);
userId = obj.getJSONObject("info").getInt("user_id");
isWait = (wait.equals("true"));
while (isWait && tries < k_MaxThreadTries) {
url = builtURL();
jsonPageStr = client.doGetRequest(url);
obj = new JSONObject(jsonPageStr);
if (!(obj.equals("") || obj.equals(null))) {
isWait = (wait.equals("true"));
}
tries++;
try {
Thread.sleep(k_ThreadSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(tries == k_MaxThreadTries) {
//exit the App
onMyDestroy();
}
}
private String builtURL() {}
private void getLocation() {
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
//gps.showSettingsAlert();
showSettingsAlert();
}
gps.stopUsingGPS();
}
public void showSettingsAlert(){
runOnUiThread(new Runnable() {
@Override
public void run() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
MainActivity.this.startActivity(intent);
hasBeenNoGps = true;
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
hasBeenNoGps = true;
onMyDestroy();
}
});
// Showing Alert Message
alertDialog.show();
}
});
}
@Override
protected void onPostExecute(ArrayList<Song> aVoid) {
super.onPostExecute(aVoid);
公共类StarTask扩展了AsyncTask{
最终int k_线程睡眠时间=3000;
最终整数k_max=7;
双纬度=0;
双经度=0;
全球定位系统;
测试主客户端;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
gps=新的GP斯特拉克(getApplication());
}
@凌驾
受保护的ArrayList doInBackground(无效…参数){
ArrayList=新建ArrayList();
client=newtestmain();
int=0;
o串;
getLocation();
字符串url=builtURL();
试一试{
字符串jsonPageStr=client.doGetRequest(url);
JSONObject obj=新的JSONObject(jsonPageStr);
userId=obj.getJSONObject(“info”).getInt(“user_id”);
isWait=(wait.equals(“true”);
while(isWait&&trys
您正在showSettingsAlert()
上执行UI操作,该操作在AsyncTask
的doInBackground()
过程中调用。允许的方法是使所有涉及UI的操作远离doInBackground()
。在这里,您可以从getLocation()中删除else条件
而不是在preExecute()上实现它
public class StarTask extends AsyncTask<Void,Void,ArrayList<Song>>{
final int k_ThreadSleepTime = 3000;
final int k_MaxThreadTries = 7;
double latitude = 0;
double longitude = 0;
GPSTracker gps;
TestMain client;
@Override
protected void onPreExecute() {
super.onPreExecute();
gps = new GPSTracker(getApplication());
if (!gps.canGetLocation()) {
showSettingsAlert();
}
}
@Override
protected ArrayList<Song> doInBackground(Void... params) {
ArrayList<Song> list = new ArrayList();
client = new TestMain();
int tries = 0;
String o;
getLocation();
String url = builtURL();
try {
String jsonPageStr = client.doGetRequest(url);
JSONObject obj = new JSONObject(jsonPageStr);
userId = obj.getJSONObject("info").getInt("user_id");
isWait = (wait.equals("true"));
while (isWait && tries < k_MaxThreadTries) {
url = builtURL();
jsonPageStr = client.doGetRequest(url);
obj = new JSONObject(jsonPageStr);
if (!(obj.equals("") || obj.equals(null))) {
isWait = (wait.equals("true"));
}
tries++;
try {
Thread.sleep(k_ThreadSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(tries == k_MaxThreadTries) {
//exit the App
onMyDestroy();
}
}
private void getLocation() {
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
}
gps.stopUsingGPS();
}
公共类StarTask扩展了AsyncTask{
最终int k_线程睡眠时间=3000;
最终整数k_max=7;
双纬度=0;
双经度=0;
全球定位系统;
测试主客户端;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
gps=新的GP斯特拉克(getApplication());
如果(!gps.canGetLocation()){
showSettingsAlert();
}
}
@凌驾
受保护的ArrayList doInBackground(无效…参数){
ArrayList=新建ArrayList();
client=newtestmain();
int=0;
o串;
getLocation();
字符串url=builtURL();
试一试{
字符串jsonPageStr=client.doGetRequest(url);
JSONObject obj=新的JSONObject(jsonPageStr);
userId=obj.getJSONObject(“info”).getInt(“user_id”);
isWait=(wait.equals(“true”);
while(isWait&&trys