简体   繁体   English

将Doubles的ArrayList传递给另一个类

[英]Passing an ArrayList of Doubles to another class

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. 我一直在寻找一些帮助,最近几天我试图将一个ArrayList of Doubles从AsyncTask传递到另一个类,但没有成功,现在我想我应该将该数组传递回mainActivity,然后将其运行到从那里的ProxAlert类。 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. 我去了将它转移到主线程,但我必须将其设置为Parcelable,但我不确定这是最直接的方法。 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 它在onCreate()之前实例化,这就是为什么我认为最好将数组传递回主要活动,然后传递给proxAlert.class的原因

The exception you get is the result of trying to get system services in the ProxAlert activity before the activity's onCreate() was called. 您得到的异常是在调用活动的onCreate()之前尝试在ProxAlert活动中获取系统服务的结果。 Your AsyncTask doesn't respect the activity life cycle. 您的AsyncTask不遵守活动生命周期。 You cannot instantiate an activity with new . 您不能使用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 如果要使用其他活动,则必须按照本文中所述的方式使用Intent: 在Android上将ArrayList <Double>从一个活动传递到另一个活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM