2017-04-09 51 views
0

我剛剛在Android上使用SQLite,我在這裏有一些錯誤。Android的IndexOutOfBoundsException:索引:1,大小:1

我有這樣的應用程序:

http://imgur.com/gallery/The7Q

有了下面的代碼,我可以點擊保存按鈕,在列表中的第一個項目,在這種情況下,倫敦,然後去的位置地點。

但是當我添加第二個地方,並嘗試上面我得到一個:

java.lang.IndexOutOfBoundsException:指標:1,尺寸:1

在此行中:

//Centralize the selected item 
     Orientation selectedLocation = geocodingResult.getResults().get(getIntent().getIntExtra(SELECTED_POSITION, 1)).getGeometry().getLocation(); 

如何解決這個問題?

地圖活動:

public class MapActivity extends AppCompatActivity { 

private GoogleMap map; 
private GeocodeResult geocodingResult; 

List<Address> listAddresses; 

String address = ""; 

public LatLng latlng; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activity_map_googlemap)).getMap(); 

    addMarkers(); 
} 

// Show the markers on the map 
public void addMarkers() { 
    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
    geocodingResult = getIntent().getParcelableExtra(MainActivity.RESULT); 

    if (geocodingResult != null) { 
     // For each Geocoding object, get the results markers 
     for (Geocoding geocoding : geocodingResult.getResults()) { 
      map.addMarker(new MarkerOptions() 
        .position(new LatLng(geocoding.getGeometry().getLocation().getLat(), geocoding.getGeometry().getLocation().getLng())) 
        .icon(BitmapDescriptorFactory.defaultMarker()) 
        .title(geocoding.getFormattedAddress()) //location name that will be shown when click the marker 
        .snippet(String.valueOf(geocoding.getGeometry().getLocation().getLat()) 
          + ", " 
          + String.valueOf(geocoding.getGeometry().getLocation().getLng())) //coordinates that will be shown when click the marker 
      ); 
      try { 
       latlng = new LatLng(geocoding.getGeometry().getLocation().getLat(), geocoding.getGeometry().getLocation().getLng()); 
       listAddresses = geocoder.getFromLocation(geocoding.getGeometry().getLocation().getLat(), geocoding.getGeometry().getLocation().getLng(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Log.d("LIST", String.valueOf(listAddresses)); 
     } 


     //Centralize the selected item 
     Orientation selectedLocation = geocodingResult.getResults().get(getIntent().getIntExtra(SELECTED_POSITION, 1)).getGeometry().getLocation(); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLocation.getLat(), selectedLocation.getLng()), 4)); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_main, menu); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 

    if (item.getItemId() == R.id.save) { 

     try { 

      List<Address> listAddresses = geocoder.getFromLocation(latlng.latitude, latlng.longitude, 1); 

      if (listAddresses != null && listAddresses.size() > 0) { 

       if (listAddresses.get(0).getLocality() != null) { 

        if (listAddresses.get(0).getPostalCode() != null) { 

         address += listAddresses.get(0).getLocality() + " "; 

        } 

        address += listAddresses.get(0).getPostalCode(); 

       } 
      } 
      Log.d("LIST", String.valueOf(listAddresses)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if (address == "") { 

      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm yyyy-MM-dd"); 

      address = sdf.format(new Date()); 
     } 

     DBController crud = new DBController(getBaseContext()); 
     String result; 
     result = crud.insertData(address, latlng.latitude, latlng.longitude); 
     Log.d("ADDRESS", address); 
     Log.d("LAT", String.valueOf(latlng.latitude)); 
     Log.d("LNG", String.valueOf(latlng.longitude)); 
     Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); 
    } 

    return super.onOptionsItemSelected(item); 
}// END MENU 

主營:

public class MainActivity extends AppCompatActivity { 

public final static String RESULT = "listGeocoding"; 
public final static String SELECTED_POSITION = "selectedPosition"; 

public static ListView listView; 
EditText edtSearch; 
Button btnSearch; 

GeocodeResult geocodingResult; 
Dialog dialogProgress; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    edtSearch = (EditText) findViewById(R.id.edtSearch); 
    btnSearch = (Button) findViewById(R.id.btnSearch); 

    listView = (ListView) findViewById(R.id.listView); 

    setListeners(); 

}// END ON CREATE 

// Set Listeners to the activity 
private void setListeners() { 

    //region Button click jump to SearchAddress method 
    btnSearch.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      searchAddress(); 
     } 
    }); 
    //endregion 

    //region List View Click jump to MapActivity 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent intent = new Intent(MainActivity.this, MapActivity.class); 
      intent.putExtra(RESULT, geocodingResult); 
      intent.putExtra(SELECTED_POSITION, position); 
      startActivity(intent); 
     } 
    }); 
    //endregion 
}// END METHOD 

//region Deal with orientation changes 
@Override 
protected void onSaveInstanceState(Bundle outState) { 
    outState.putParcelable(RESULT, geocodingResult); 
    super.onSaveInstanceState(outState); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    geocodingResult = savedInstanceState.getParcelable(RESULT); 

    if (geocodingResult != null && geocodingResult.getResults().size() > 0) { 
     loadResult(); 
    } 
} 
//endregion 

//region Search method 
private void searchAddress() { 
    //region Deal with the keyboard and user input 
    CommonUtils.hideKeyboard(MainActivity.this, edtSearch); 

    // Ask the user for the address if not provided 
    if (edtSearch.getText().toString().equals("")) { 
     Toast.makeText(MainActivity.this, "Please enter an address", Toast.LENGTH_SHORT).show(); 
     return; // pause and await for response 
    } 
    //endregion 

    listView.setVisibility(View.GONE); 
    showProgress(); 

     //region Search the address 
     GoogleMaps service = ServiceGenerator.createService(GoogleMaps.class, "http://maps.googleapis.com"); // API URL 
     service.getGeocoding(edtSearch.getText().toString(), true, new Callback<GeocodeResult>() { 
      @Override 
      public void success(GeocodeResult googleGeocodingResult, Response response) { 
       hideProgress(); 
       geocodingResult = googleGeocodingResult; 

       if (geocodingResult.getResults().size() > 0) { 
        loadResult(); 
       } else { 
        Toast.makeText(MainActivity.this, "Didn't find a place.", Toast.LENGTH_SHORT).show(); 
       } 
      } 

      @Override 
      public void failure(RetrofitError error) { 
       hideProgress(); 
       Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     //endregion 
} 

private void loadResult() { 
    listView.setVisibility(View.VISIBLE); 
    listView.setAdapter(new Adapter(MainActivity.this, geocodingResult.getResults())); 
} 
//endregion 

//region Progress Dialog 
private void showProgress() { 
    if (dialogProgress == null) { 
     dialogProgress = new Dialog(this); 
     dialogProgress.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialogProgress.setContentView(R.layout.custom_progress); 
     dialogProgress.setCancelable(false); 
    } 
    dialogProgress.getWindow().getDecorView().getRootView(); 
    dialogProgress.show(); 
} 

private void hideProgress() { 
    if (dialogProgress != null) { 
     dialogProgress.dismiss(); 
     dialogProgress = null; 
    } 
} 
//endregion 

//region Menu 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_main, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if (item.getItemId() == R.id.save){ 
     Intent intent = new Intent(getApplicationContext(), Query.class); 
     intent.putExtra(RESULT, geocodingResult); 
     startActivity(intent); 
    } 
    return super.onOptionsItemSelected(item); 
} 

查詢:

import static com.arthurabreu.memorableplaces.MainActivity.RESULT; 
import static com.arthurabreu.memorableplaces.MainActivity.SELECTED_POSITION; 


/** 
* Created by blitz on 4/8/2017. 
*/ 

public class Query extends AppCompatActivity { 

    private ListView list; 
    private GeocodeResult geocodingResult; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list_view_query); 

     DBController crud = new DBController(getBaseContext()); 
     Cursor cursor = crud.loadData(); 

     String[] titles = new String[] {SQLite.ID, SQLite.KEY_TITLE}; 
     int[] idViews = new int[] {R.id.idNumber, R.id.idTitle}; 


     SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), 
       R.layout.adapter_query_layout,cursor,titles,idViews, 0); 
     list = (ListView)findViewById(R.id.listView); 
     list.setAdapter(adapter); 


     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

       final Cursor c = ((SimpleCursorAdapter)list.getAdapter()).getCursor(); 
       c.moveToPosition(position); 
       String place = c.getString(1); //Get the name of the place saved in the cursor 
       Log.d("TEST", String.valueOf(c.getString(1))); 

       //region Search the address 
       GoogleMaps service = ServiceGenerator.createService(GoogleMaps.class, "http://maps.googleapis.com"); // API URL 
       service.getGeocoding(place, true, new Callback<GeocodeResult>() { 
        @Override 
        public void success(GeocodeResult googleGeocodingResult, Response response) { 

         geocodingResult = googleGeocodingResult; 

         if (geocodingResult.getResults().size() > 0) { 
          Intent intent = new Intent(getApplicationContext(), MapActivity.class); 
          intent.putExtra(RESULT, geocodingResult); 
          intent.putExtra(SELECTED_POSITION, position); 
          Log.d("POSITION", String.valueOf(position)); 
          startActivity(intent); 
         } else { 
          Toast.makeText(getApplicationContext(), "Didn't find a place.", Toast.LENGTH_SHORT).show(); 
         } 
        } 

        @Override 
        public void failure(RetrofitError error) { 

         Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
        } 
       }); 
       //endregion 

      }// END ON CLICK 
     }); 

    }// END ONCREATE 
}// ENDMAIN 

回答

1

出現你的錯誤時,編碼無法達到特定的索引。您可以查看此thread

IndexOutOfBound異常表示您遇到問題,因爲您嘗試訪問不存在或爲空(不爲空)的索引。例如,如果你有一個只有兩個元素的數組,所以它只有索引0和1,並且你試圖訪問索引2,你會得到一個IndexOutOfBoundException,因爲索引2不存在。如果您創建了一個由10個元素組成的數組,並且只填充5個數據,則索引4-9將爲空,訪問這些元素可能會導致IndexOutOfBoundException。

這裏有一個關於如何解決此問題可能的解決方法:

如果您使用的是IDE它可以幫助通過移除引發語句您調試問題。當涉及到異常時,會拋出語句「傳遞低價」,但您實際上並沒有通過使用拋出來處理異常。 try-catch語句更好地處理異常,因爲它們縮小了問題所在的區域(因爲它們在「測試」框中測試代碼。)

引發它們的位置,但對於調試try-catch可能更多。有用

其他參考:

+0

我真的很感謝所有的幫助。通過參考和解釋,我發現了這個問題。真是愚蠢的tbh。我試圖點擊列表上的一個項目,並在我試圖在數據庫中獲得他的ID時獲得它在列表中的位置。 XD – BlitzkriegBlue

相關問題