mainactivity中的Intent回收



我试图在我的MainActivity类上开始一个意图从EditText字段收到一个条目后。我传递一些额外的数据。在我的MainActivity我检查是否有任何额外的意图。如果是这样,我将在异步任务中处理数据。问题是,我的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打开灯。"餐厅",即上述活动。下一次通过我进入我的MainActivity,它也通过下面的逻辑两次,每次在我的logcat上前面有以下消息:标签= MapActivity文本=回收地图对象。

下面是MainActivity处理从上面的activity接收到的额外的Intent。

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

下面是代码中,它似乎我得到了nullexception,但我得到的第一个日志"getrerestaurantlate =",但我没有得到第二个日志,这是"getrerestauranttry"。我不知道在这两个日志之间可以发生nullexception。此外,我从其他来源进入这个例程,它可以工作,只是在这个上下文中不起作用。

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

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

最新更新