1

I was looking for some help I've spent the last few days trying to pass an ArrayList of Doubles into another class from an AsyncTask with no success and I am now thinking I should pass the array back to the mainActivity and then run it to the ProxAlert class from there. Is this the way to do it or am I over complicating it? Thanks for your help.

try{
        ArrayList<LatLonPair> nPosition = new ArrayList<LatLonPair>();
        JSONArray jArray = new JSONArray(result);


        System.out.println("Length"+ jArray.length());
        Log.d("DB","Length"+jArray.length());

        for(int i=0; i<jArray.length(); i++){

                JSONObject json_data = null;

                json_data = jArray.getJSONObject(i);
                int eventID = i +1;
                String title = json_data.getString("Title");
                double latitude = json_data.getDouble("Lat"); 
                double longitude = json_data.getDouble("Lon"); 

                LatLonPair p = new LatLonPair(latitude , longitude);
                nPosition.add(p);


                //System.out.println(eventID+"&"+latitude+"&"+longitude);
                System.out.println("this" + p.printPoints());                        
        }    

    }       
    catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
            Log.e("log_tag","Failed data as:\n"+result);                
    }    

        return nPosition;           
}

@Override
protected void onPostExecute(ArrayList<LatLonPair> result) {
    new ProxAlert().registerIntents(result);
    super.onPostExecute(result);
    //Intent i = new Intent();
    //i.putParcelableArrayListExtra("nPositon",(ArrayList<LatLonPair>) result);

    //startARActivity();
}

I went to transfer it to the main thread but I would have to make it Parcelable and I'm just unsure as the most direct approach. Sorry the error that I get is

09-18 12:27:16.605: E/AndroidRuntime(20122): java.lang.IllegalStateException: System      services not available to Activities before onCreate()
 09-18 12:27:16.605: E/AndroidRuntime(20122):   at android.app.Activity.getSystemService(Activity.java:3562)
 09-18 12:27:16.605: E/AndroidRuntime(20122):   at com.example.test.ProxAlert.setProximityAlert(ProxAlert.java:51)
09-18 12:27:16.605: E/AndroidRuntime(20122):    at com.example.test.ProxAlert.registerIntents(ProxAlert.java:37)
09-18 12:27:16.605: E/AndroidRuntime(20122):    at com.example.test.retrieveDB.onPostExecute(retrieveDB.java:116)
09-18 12:27:16.605: E/AndroidRuntime(20122):    at com.example.test.retrieveDB.onPostExecute(retrieveDB.java:1)
09-18 12:27:16.605: E/AndroidRuntime(20122):    at android.os.AsyncTask.finish(AsyncTask.java:417)
09-18 12:27:16.605: E/AndroidRuntime(20122):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
09-18 12:27:16.605: E/AndroidRuntime(20122):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)

Its instantiating before the onCreate(), thats why i was thinking it might be best to passed the array back to the main activity and then to the proxAlert.class

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
Aine
  • 11
  • 2
  • is there any error? what happens when you try to get `result` in `ProxAlert`? – Emmanuel Sep 17 '13 at 18:55
  • 1
    possible duplicate of [Pass ArrayList from one activity to another activity on android](http://stackoverflow.com/questions/11648312/pass-arraylistdouble-from-one-activity-to-another-activity-on-android) – Simon Sep 17 '13 at 19:01
  • @Simon: read the question again. This is not a duplicate – gunar Sep 18 '13 at 12:09
  • Any reason why you aren't using [LatLng](http://developer.android.com/reference/com/google/android/gms/maps/model/LatLng.html) class? It's available through the Google Play Services. – Steve Benett Sep 18 '13 at 12:26

1 Answers1

2

The exception you get is the result of trying to get system services in the ProxAlert activity before the activity's onCreate() was called. Your AsyncTask doesn't respect the activity life cycle. You cannot instantiate an activity with new.

If you want to use other activites, then you must use Intents in the way it is described in this post: Pass ArrayList<Double> from one activity to another activity on android

Community
  • 1
  • 1
allprog
  • 16,540
  • 9
  • 56
  • 97
  • The array is made of LatLng pairs, I have 13 pairs that need to be passed into a proximity alert? I thought by putting them into an array they would be easier to pass. I'm fairly new to java and I think I've just over complicated it – Aine Sep 18 '13 at 13:56
  • 1
    You need to understand the lifecycle model and interactions that are possible. The question I linked in shows you how to pass data between activities using Intents. – allprog Sep 18 '13 at 14:13
  • OK I have created a `ArrayList` and tried to pass it using intent but i can't run as `myActivity.this` wont work, I replaced it with activity and it runs but an error appears pointing back to `Intent i = new Intent(activity, ProxAlert.class);` – Aine Sep 18 '13 at 15:50
  • I think I worked out the problem the datasets didn't match, because the locations in the database were not written from a LocationManager they were missing some information required to used the proximityAlert so they couldn't be passed.. Thanks for all the help – Aine Sep 24 '13 at 22:32
  • I'm glad that you figured out. It's always good when an issue gets solved. :) I'm looking forward to helping more in the future. However, your code and the stack trace reflect the issues that I pointed out so in my opinion I answered your question. For future reference it would be better marking it the solution provided that you agree with my analysis. – allprog Sep 24 '13 at 22:48