我試圖從EditText字段接收到一個條目後,在我的MainActivity類中啓動一個意圖。我通過額外的數據傳遞一些數據。在我的MainActivity中,我檢查是否有任何來自Intent的額外內容。如果是的話,我在一個異步任務中處理數據。問題是我的MainActivity經歷了兩次檢查從一個意圖和最終程序炸彈的額外過程。這非常令人困惑,我會在下面展示程序如何流動。首先在MainActivity下的另一個活動中啓動意圖。MainActivty中的意圖回收
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 [email protected]0fc2f88
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_a[email protected]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_a[email protected]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 [email protected]
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)
所以我的問題是,當我在上面的第一個活動中輸入數據時,程序爲什麼會經歷兩次邏輯?這是非常混亂和最終的結果,我得到一個erorr在MessageQueue callbackL handleReceiveCallback和IndexOutOfBoundsException異常說。
如果有人需要關於這個令人困惑的問題的更多信息,請讓我知道。
下面是它出現的代碼我得到了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++;
}
你可以請張貼完整的堆棧跟蹤嗎?並請指出哪一行有哪些行號。 – 2013-03-20 21:28:10
爲什麼拉斯在AsyncTask中與您在意圖中獲得的內容相比會有所不同?一個說40.9040412902832,後一個40.6936488?爲什麼你將double參數傳遞爲String而不是double? – 2013-03-20 21:34:05
我意識到,當我將它傳遞給asnyctask時,我選擇了錯誤的lat值,並且糾正了這個錯誤。我沒有理由使用字符串,而不是使用double,除非我是新手,因爲您可以告訴並且正在嘗試學習傳遞數據的不同解析方法。我是一名C#程序員,試圖將其轉換爲Java。謝謝你的建議。 – Dave 2013-03-21 00:22:04