简体   繁体   中英

onCreate() being called infinitely

I'm new to android and I cannot figure out why my onCreate() in my activity is being called over and over. In my MainActivity I have this function search, that is called when a button is pressed.

private void search(View v){
    //Get input fields if null then dont restrict search
    ... 
    ...
    Intent i = new Intent(getApplicationContext(), PersonActivity.class);
    i.putExtra("miles",numMiles);
    i.putExtra("min",numMin);
    startActivity(i);
}

Then in my PersonActivity Class I have this onCreate Method.

public class PersonActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
public ArrayList<PersonInfo> people;
private LayoutInflater inflater;
private Context _context;
public PersonInfoListAdapter _adapter;
public static ListView _list;
public int _position;
private static final String APP_ID = "4cae6e4d7d89bc4b02196b352ae91650";
// Find this in your developer console
private static final String API_KEY = "4A3243EECEA842F7B1FC6E7A4B00DA7A";

public PersonActivity() {}

@Override
protected void onCreate(Bundle savedInstanceState) {;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person);
    View listV = findViewById(R.id.friendList);
    _list = (ListView) listV;
    CMApiCredentials.initialize(APP_ID, API_KEY, getApplicationContext());

    try {
        people = new ArrayList<PersonInfo>();

        Context context = _list.getContext();
        _adapter = new PersonInfoListAdapter(context, people);
        _list.setAdapter(_adapter);

    } catch (Exception e) {
        e.printStackTrace();
    }

    _list.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            View parentRow = (View) view.getParent();
            return true;
        }
    });

    Integer numMin = -1;
    Integer numMiles = -1;
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        numMin = extras.getInt("min");
        numMiles = extras.getInt("miles");
        Log.d("Person Activity", "Miles" + numMiles.toString());
    }

    queryForPeople(numMiles, numMin);
}

private void queryForPeople(Integer numMiles, Integer numMin){
    //Get users location
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new MyLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
    Location lastKnownLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);


    double lat = lastKnownLoc.getLatitude();
    double lon = lastKnownLoc.getLongitude();

    Intent myIntent = new Intent(this, PersonActivity.class);
    startActivity(myIntent);
    //search for friends
    PersonInfo.searchObjects(this, SearchQuery.filter("location").near(lon, lat).within(numMiles, DistanceUnits.mi).searchQuery(),
            new Response.Listener<CMObjectResponse>() {
                @Override
                public void onResponse(CMObjectResponse response) {
                    List<CMObject> objectList = response.getObjects();
                    for (CMObject o : objectList) {
                        PersonInfo p = (PersonInfo) o;
                        people.add(p);
                        Log.d(TAG, "Person was found: " + p.getName());
                    }

                }
            });

    _adapter.notifyDataSetChanged();
}


private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {
        Toast.makeText(
                getBaseContext(),
                "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                        + loc.getLongitude(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}

The problem is this onCreate() in my personActivity is being called over an over. And I cannot figure out why. queryForPeople makes a call to and API that returns information that I use to populate the people ArrayList that is used in the adapter.

Any Ideas?

Your queryForPeople method calls startActivity on the activity you're already running from. So this will end up being infinitely recursive. I don't see why you would want to start yourself from anywhere in your activity, it just doesn't make sense. Unless you intended something else with those two lines, simply remove them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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