我在从EditText字段接收条目后尝试在我的MainActivity类上启动一个intent。我传递了一些额外的数据。在我的MainActivity中,我检查是否有来自Intent的额外内容。如果是这样,我在异步任务中处理数据。问题是我的MainActivity经历了两次检查来自Intent的额外内容以及最终程序炸弹的过程。这非常令人困惑,我将在下面展示程序如何流动。下面首先是在MainActivity下面的另一个活动中启动意图。
thisLocation = (EditText) findViewById(R.id.restlocation);
thisLocation.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String location = thisLocation.getText().toString();
Log.d("Location in key listener", location);
try
{
List<Address> foundGeocode = null;
foundGeocode = new Geocoder(getApplicationContext()).getFromLocationName(location, 1);
double newlat = foundGeocode.get(0).getLatitude(); //getting latitude
double newlng = foundGeocode.get(0).getLongitude();//getting longitude
Log.d("Lat in key listener", String.valueOf(newlat));
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("lat", String.valueOf(lat));
intent.putExtra("lng", String.valueOf(lng));
startActivity(intent);
}
catch (IOException e) {
Log.e("Geocode", String.valueOf(e));
}
return true;
}
return false;
}
});
我可以从我的日志中看出,在进入MainActivity之前,处理过两次上述例程。在第一次通过监听器之后,logcat显示标签'MapActivity'和文本“onDestroy为com.example.herbx.Restaurant留下灯光”,这是上面的活动。下次通过我输入我的MainActivity时,每次在我的logcat上都会出现以下消息,也会经历以下逻辑两次:Tag = MapActivity Text =回收地图对象。
接下来是从上述活动收到的Intent附加内容的MainActivity处理。
protected void onCreate(Bubdke savedInstanceState) {
...... intialization code
Bundle extras = getIntent().getExtras();
if(extras != null)
{
lat = Float.parseFloat(extras.getString("lat"));
lng = Float.parseFloat(extras.getString("lng"));
Log.d("lat=", String.valueOf(lat));
NewMap(lat, lng); // This is an asynchronous task
}
}
下面是我的logcat转储,显示我记录某些数据的位置。
03-20 19:21:30.256: D/Location in key listener(4045): Peoria
03-20 19:21:30.519: D/Lat in key listener(4045): 40.6936488
03-20 19:21:30.616: D/MapActivity(4045): onDestroy leaving the lights on for com.example.herb4.Restaurant@40fc2f88
03-20 19:21:30.736: D/Location in key listener(4045): Peoria
03-20 19:21:30.986: D/Lat in key listener(4045): 40.6936488
03-20 19:21:31.016: I/Choreographer(4045): Skipped 45 frames! The application may be doing too much work on its main thread.
03-20 19:21:31.168: W/MapActivity(4045): Recycling dispatcher android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher@40cf8f38
03-20 19:21:31.207: V/MapActivity(4045): Recycling map object.
03-20 19:21:31.426: D/lat=(4045): 40.9040412902832
03-20 19:21:31.746: D/newmap addr=(4045): W 7th StMinonk, IL 61760
03-20 19:21:31.896: W/MapActivity(4045): Recycling dispatcher android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher@40cf8f38
03-20 19:21:31.948: V/MapActivity(4045): Recycling map object.
03-20 19:21:32.166: D/lat=(4045): 40.9040412902832
03-20 19:21:32.206: D/GetRestaurant lat=(4045): 40.9040412902832
03-20 19:21:32.387: D/newmap addr=(4045): W 7th StMinonk, IL 61760
03-20 19:21:32.446: I/Choreographer(4045): Skipped 69 frames! The application may be doing too much work on its main thread.
03-20 19:21:32.726: D/dalvikvm(4045): GREF has increased to 201
03-20 19:21:32.956: D/InputEventConsistencyVerifier(4045): KeyEvent: ACTION_UP but key was not down.
03-20 19:21:32.956: D/InputEventConsistencyVerifier(4045): in android.view.ViewRootImpl@4109f880
03-20 19:21:32.956: D/InputEventConsistencyVerifier(4045): 0: sent at 15675516000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_ENTER, scanCode=28, metaState=0, flags=0x8, repeatCount=0, eventTime=15675516, downTime=15675423, deviceId=0, source=0x101 }
03-20 19:21:33.166: E/InputEventReceiver(4045): Exception dispatching input event.
03-20 19:21:33.166: E/MessageQueue-JNI(4045): Exception in MessageQueue callback: handleReceiveCallback
03-20 19:21:33.256: E/MessageQueue-JNI(4045): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-20 19:21:33.256: E/MessageQueue-JNI(4045): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-20 19:21:33.256: E/MessageQueue-JNI(4045): at java.util.ArrayList.get(ArrayList.java:304)
所以我的问题是为什么当我在上面的第一个活动中输入数据时,程序会经历两次逻辑?这是非常令人困惑的,最终结果我得到了一个错误,在MessageQueue callbackL handleReceiveCallback和IndexOutOfBoundsException中说Exception。 如果有人需要关于这个令人困惑的问题的更多信息,请告诉我。
下面是它出现的代码我得到了nullexception但是我得到了第一个“GetRestaurant lat =”的日志,但我没有得到第二个日志“GetRestaurant try”。我不知道这两个日志之间可以发生什么样的漏洞。此外,我从其他来源进入这个例程并且它可以工作,只是在这种情况下不起作用。
List<String> result = new ArrayList<String>();
URL url;
Log.d("GetRestaurant lat=", thislat); // this gets logged
try {
Log.d("GetRestaurant try", ""); // this doesn't get logged
String query = "?lat=" + thislat + "&lng=" + thislng + "&uid=&vegkey=1";
url = new URL("http://10.0.2.2:8000/herbivorenew/webservice/frontpage1.php" + query);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String line;
int x=0;
while ((line = br.readLine()) != null) {
result = new ArrayList<String>(Arrays.asList(line.split("&")));
Log.d("GetRestaurant result=", String.valueOf(result.get(0)));
x++;
}
答案 0 :(得分:1)
onKey()被调用两次,一次按下按键,一次按下按键。你需要做的是这样的事情:
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction()!=KeyEvent.ACTION_DOWN) {
return true;
}
// here goes your code
return true;
}
return false;
}