当设备上未检测到网络时,我的AsyncTask
出现问题。
寻找有关如何解决此问题的建议,谢谢。
LogCat似乎指向for (int i = 0; i < json.length(); i++) {
,我不确定如果网络不可用,如何阻止此代码运行
的代码的的
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
try {
new ProgressTask(GPSME_Main.this).execute();
}
catch(Exception e) {
Intent callNETWORKSettingIntent = new Intent(
android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
startActivity(callNETWORKSettingIntent);
}
}
即时解析位置数据
private class ProgressTask extends
AsyncTask<String, Void, ArrayList<String>> {
ArrayList<String> arrfortextviews;
private GPSME_Main activity;
private ProgressBar progressBar1;
public ProgressTask(GPSME_Main parseMoreDetails) {
this.activity = parseMoreDetails;
context = parseMoreDetails;
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
}
private Context context;
protected void onPreExecute() {
progressBar1.setVisibility(View.VISIBLE);
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
parse_current_location = location.getLatitude() + ","
+ location.getLongitude();
}
}
@Override
protected void onPostExecute(ArrayList<String> success) {
if (arrfortextviews.size() > 0) {
progressBar1.setVisibility(View.GONE);
current_address = (success.get(0));
body_location_address_result.setText(current_address);
}
}
protected ArrayList<String> doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
arrfortextviews = new ArrayList<String>();
JSONArray json = jParser.getJSONFromUrl(google_maps_url + "latlng="
+ parse_current_location + "&sensor=true");
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String parsed_results_formatted_address = c
.getString("formatted_address");
arrfortextviews.add(parsed_results_formatted_address);
} catch (JSONException e) {
e.printStackTrace();
}
}
return arrfortextviews;
}
}
的 logcat的的的
: FATAL EXCEPTION: AsyncTask #1
: java.lang.RuntimeException: An error occured while executing doInBackground()
: at android.os.AsyncTask$3.done(AsyncTask.java:299)
: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
: at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
: at java.util.concurrent.FutureTask.run(FutureTask.java:239)
: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
: at java.lang.Thread.run(Thread.java:856)
: Caused by: java.lang.NullPointerException
: at org.greenbot.technologies.gpsme.GPSME_Main$ProgressTask.doInBackground(GPSME_Main.java:575)
: at org.greenbot.technologies.gpsme.GPSME_Main$ProgressTask.doInBackground(GPSME_Main.java:1)
: at android.os.AsyncTask$2.call(AsyncTask.java:287)
: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
: ... 4 more
答案 0 :(得分:3)
您收到nullPointerException
,因为您期待JSON
响应,但由于您没有网络,因此无法解析。
阻止此代码运行的一种方法是在进入循环之前简单地进行空检查:
if(json != null){
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String parsed_results_formatted_address = c
.getString("formatted_address");
arrfortextviews.add(parsed_results_formatted_address);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
如果是我,我会在执行代码之前检查网络连接。这可以在调用AsyncTask
:
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//if there is a connection
if (netInfo != null && netInfo.isConnectedOrConnecting()){
...put your code here...
}
最终,这取决于在没有连接时是否需要运行AsycTask
。