1

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
4

1 に答える 1