我是android的新手,我无法弄清楚为什么我的活动中的onCreate()被一遍又一遍地调用。 在我的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);
}
然后在我的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) {}
}
问题是这个onCreate()在我的personActivity被调用了。我无法弄清楚原因。 queryForPeople调用和API返回我用来填充适配器中使用的人ArrayList的信息。
任何想法?
答案 0 :(得分:2)
您的queryForPeople方法会对您已经运行的活动调用startActivity。所以这最终将无限递归。我不明白你为什么要从你活动的任何地方开始自己,这没有意义。除非你打算用这两行代替其他内容,否则只需删除它们即可。