简体   繁体   English

onCreate()被无限调用

[英]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. 我是android的新手,我无法弄清为什么一遍又一遍地调用我的onCreate()活动。 In my MainActivity I have this function search, that is called when a button is pressed. 在我的MainActivity中,我有此函数搜索,当按下按钮时会调用该函数。

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. 然后在我的PersonActivity类中,有此onCreate方法。

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. 问题是我的personActivity中的onCreate()被调用了。 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. queryForPeople调用和API,该API返回用于填充适配器中使用的人员ArrayList的信息。

Any Ideas? 有任何想法吗?

Your queryForPeople method calls startActivity on the activity you're already running from. 您的queryForPeople方法对您已经在其中运行的活动调用startActivity。 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. 除非您对这两行有其他要求,否则只需将其删除即可。

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

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