2013-03-20 47 views
0

我试图从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++; 
} 
+0

你可以请张贴完整的堆栈跟踪吗?并请指出哪一行有哪些行号。 – 2013-03-20 21:28:10

+0

为什么拉斯在AsyncTask中与您在意图中获得的内容相比会有所不同?一个说40.9040412902832,后一个40.6936488?为什么你将double参数传递为String而不是double? – 2013-03-20 21:34:05

+0

我意识到,当我将它传递给asnyctask时,我选择了错误的lat值,并且纠正了这个错误。我没有理由使用字符串,而不是使用double,除非我是新手,因为您可以告诉并且正在尝试学习传递数据的不同解析方法。我是一名C#程序员,试图将其转换为Java。谢谢你的建议。 – Dave 2013-03-21 00:22:04

回答

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; 
    } 
+0

感谢您的反馈。我做了修正并重新编译,现在我只得到一个响应,但在上面的logcat中显示的末尾仍然出现相同的错误。最后的错误似乎发生在asyntask程序中。你知道什么可能导致上述错误? – Dave 2013-03-20 20:51:37

+0

这是一个java.lang.IndexOutOfBoundsException:无效的索引0,大小为0,所以我想你正在访问一个空列表或索引为0的东西,但是你的堆栈跟踪并没有显示你的代码中的行和你的asynctask代码没有发布,所以没有办法回答这个问题。您应该检查您的列表或数组是否正确初始化,或者发布堆栈跟踪和相关代码 – 2013-03-20 21:04:58

+0

我已将逻辑向下移动,以确保执行asyntask是我在onCreate过程中执行的最后一件事,似乎使它工作。谢谢你的帮助。 – Dave 2013-03-20 21:39:07