0

在收到来自 EditText 字段的条目后,我试图在我的 MainActivity 类上启动一个意图。我通过一些额外的数据。在我的 MainActivity 中,我检查是否有来自 Intent 的任何额外内容。如果是这样,我会在异步任务中处理数据。问题是我的 MainActivity 经历了两次检查 Intent 中的额外内容并最终导致程序炸弹的过程。这非常令人困惑,我将在下面展示程序的流程。下面首先是在 MainActivity 下方的另一个活动中启动 Intent。

   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 leave light on for com.example.herbx.Restaurant”的文本,即上述活动。下一次通过我进入我的 MainActivity 时,它每次也会通过以下逻辑两次,然后在我的 logcat 上显示以下消息:Tag = MapActivity Text=Recycling map object。

接下来是 MainActivity 处理从上述活动中接收到的 Intent 中的 extras。

    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。
如果有人需要关于这个令人困惑的问题的更多信息,请告诉我。

下面是出现空异常的代码,但我得到了“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++;
}  
4

1 回答 1

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;
    }
于 2013-03-20T20:35:16.637 回答