当我尝试从数组列表中捕获纬度和经度并将其传递给Viewmap.java文件中名为displaymarkers()的函数时,我收到错误。
我添加了我的代码和错误。任何人都可以帮我解决这个错误。
数据库处理程序(Dbhandler.java)类
package lk.adspace.jaffnatemples;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Dbhandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "jaffnatempletest";
// Temple table name
private static final String TABLE_TEMPLE = "templ";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TMPNAME = "temple_name";
private static final String KEY_TMPTYPE = "temple_type";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private static final String KEY_IMGNAME = "image_name";
private static final String KEY_YEARBUILD = "year_build";
private static final String KEY_ADDRESS = "address";
private static final String KEY_CITY = "city";
private static final String KEY_EMAIL = "email";
private static final String KEY_WEB = "website";
private static final String KEY_TEL1 = "telephone1";
private static final String KEY_TEL2 = "telephone2";
private static final String KEY_DESCRI = "Description";
private final ArrayList<kovil> temple_list = new ArrayList<kovil>();
public Dbhandler (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TEMPLE_TABLE = "CREATE TABLE " + TABLE_TEMPLE + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + KEY_TMPNAME + " TEXT," + KEY_TMPTYPE + " TEXT," + KEY_LATITUDE + " TEXT," + KEY_LONGITUDE + " TEXT," + KEY_IMGNAME + " TEXT,"
+ KEY_YEARBUILD + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_CITY + " TEXT," + KEY_EMAIL + " TEXT," + KEY_WEB + " TEXT," + KEY_TEL1 + " TEXT," + KEY_TEL2 + " TEXT,"
+ KEY_DESCRI + " TEXT" + ")";
db.execSQL(CREATE_TEMPLE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEMPLE);
// Create tables again
onCreate(db);
}
// Adding new temple
public void Add_Temple(kovil Kovil) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TMPNAME, Kovil.gettemplename());
values.put(KEY_TMPTYPE, Kovil.gettempletype());
values.put(KEY_LATITUDE, Kovil.getlatitude());
values.put(KEY_LONGITUDE, Kovil.getlongitude());
values.put(KEY_IMGNAME, Kovil.getimage_name());
values.put(KEY_YEARBUILD, Kovil.getyear_build());
values.put(KEY_ADDRESS, Kovil.getaddress());
values.put(KEY_CITY, Kovil.getcity());
values.put(KEY_EMAIL, Kovil.getemail());
values.put(KEY_WEB, Kovil.getwebsite());
values.put(KEY_TEL1, Kovil.gettelephone1());
values.put(KEY_TEL2, Kovil.gettelephone2());
values.put(KEY_DESCRI, Kovil.getDescription());
// Inserting Row
db.insert(TABLE_TEMPLE, null, values);
db.close(); // Closing database connection
}
// Getting single contact
kovil Get_Temple(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_TEMPLE, new String[] { KEY_ID,
KEY_TMPNAME, KEY_TMPTYPE, KEY_LATITUDE, KEY_LONGITUDE, KEY_IMGNAME, KEY_YEARBUILD, KEY_ADDRESS, KEY_CITY, KEY_EMAIL, KEY_EMAIL, KEY_WEB, KEY_TEL1, KEY_TEL2, KEY_DESCRI }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
kovil Kovil = new kovil(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13));
// return contact
cursor.close();
db.close();
return Kovil;
}
// Getting All Contacts
public ArrayList<kovil> Get_Temple(String temple_type, int Limit) {
try {
temple_list.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_TEMPLE +" WHERE temple_type= " + temple_type +" LIMIT " + Limit;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
System.out.print("CALLED");
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
kovil Kovil = new kovil();
Kovil.setID(Integer.parseInt(cursor.getString(0)));
Kovil.settemplename(cursor.getString(1));
Kovil.settempletype(cursor.getString(2));
Kovil.setlatitude(cursor.getString(3));
Kovil.setlongitude(cursor.getString(4));
Kovil.setimage_name(cursor.getString(5));
Kovil.setyear_build(cursor.getString(6));
Kovil.setaddress(cursor.getString(7));
Kovil.setcity(cursor.getString(8));
Kovil.setemail(cursor.getString(9));
Kovil.setwebsite(cursor.getString(10));
Kovil.settelephone1(cursor.getString(11));
Kovil.settelephone2(cursor.getString(12));
Kovil.setDescription(cursor.getString(13));
// Adding contact to list
temple_list.add(Kovil);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
db.close();
return temple_list;
} catch (Exception e) {
// TODO: handle exception
Log.e("all_temples", "" + e);
}
return temple_list;
}
public String collect(String temptype, String limit){
SQLiteDatabase ourDatabase = this.getWritableDatabase();
String result="";
String []column =new String[]{KEY_ID,KEY_TMPNAME,KEY_TMPTYPE,KEY_LATITUDE,KEY_LONGITUDE,KEY_IMGNAME,KEY_YEARBUILD,KEY_ADDRESS,KEY_CITY,KEY_EMAIL,KEY_WEB,KEY_TEL1,KEY_TEL2,KEY_DESCRI};
Cursor c=ourDatabase.query("templ", column, null, null, null, null,null, limit);
c.moveToFirst();
int iKEY_ID = c.getColumnIndex(KEY_ID);
int iKEY_TMPNAME= c.getColumnIndex(KEY_TMPNAME);
int iKEY_TMPTYPE= c.getColumnIndex(KEY_TMPTYPE);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
if (c.getString(iKEY_TMPTYPE).equals(temptype)){
result = result+c.getString(iKEY_ID)+"\t\t"+c.getString(iKEY_TMPNAME)+"\t\t"+c.getString(iKEY_TMPTYPE)+"\n";
}
}
return result;
}
// Getting contacts Count
public int Get_Total_Temple() {
String countQuery = "SELECT * FROM " + TABLE_TEMPLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
db.close();
return count;
}
}
我的Kovil。 Java类
package lk.adspace.jaffnatemples;
public class kovil {
//public variables
public int _id;
public String _temple_name;
public String _temple_type;
public String _latitude;
public String _longitude;
public String _image_name;
public String _year_build;
public String _address;
public String _city;
public String _email;
public String _website;
public String _telephone1;
public String _telephone2;
public String _Description;
public Object temple_name;
//empty constructor
public kovil (){
}
// int id, String temple_name, String temple_type, String latitude, String longitude, String image_name, String year_build, String address, String city, String email, String website, String telephone1, String telephone2, String Description
public kovil(int id, String temple_name, String temple_type, String latitude, String longitude, String image_name, String year_build, String address,
String city, String email, String website, String telephone1, String telephone2, String Description) {
// TODO Auto-generated constructor stub
this._id= id;
this._temple_name=temple_name;
this._temple_type=temple_type;
this._latitude=latitude;
this._longitude=longitude;
this._image_name=image_name;
this._year_build=year_build;
this._address=address;
this._city=city;
this._email=email;
this._website=website;
this._telephone1=telephone1;
this._telephone2=telephone2;
this._Description=Description;
}
public kovil(String temple_name, String temple_type, String latitude, String longitude, String image_name, String year_build, String address,
String city, String email, String website, String telephone1, String telephone2, String Description) {
// TODO Auto-generated constructor stub
this._temple_name=temple_name;
this._temple_type=temple_type;
this._latitude=latitude;
this._longitude=longitude;
this._image_name=image_name;
this._year_build=year_build;
this._address=address;
this._city=city;
this._email=email;
this._website=website;
this._telephone1=telephone1;
this._telephone2=telephone2;
this._Description=Description;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String gettemplename() {
return this._temple_name;
}
public void settemplename(String temple_name) {
this._temple_name=temple_name;
}
public String gettempletype() {
return this._temple_type;
}
public void settempletype(String temple_type) {
this._temple_type=temple_type;
}
public String getlatitude() {
return this._latitude;
}
public void setlatitude(String latitude) {
this._latitude=latitude;
}
public String getlongitude() {
return this._longitude;
}
public void setlongitude(String longitude) {
this._longitude=longitude;
}
public String getimage_name() {
return this._image_name;
}
public void setimage_name(String image_name) {
this._image_name=image_name;
}
public String getyear_build() {
return this._year_build;
}
public void setyear_build(String year_build) {
this._year_build=year_build;
}
public String getaddress() {
return this._address;
}
public void setaddress(String address) {
this._address=address;
}
public String getcity() {
return this._city;
}
public void setcity(String city) {
this._city=city;
}
public String getemail() {
return this._email;
}
public void setemail(String email) {
this._email=email;
}
public String getwebsite() {
return this._website;
}
public void setwebsite(String website) {
this._website=website;
}
public String gettelephone1() {
return this._telephone1;
}
public void settelephone1(String telephone1) {
this._telephone1=telephone1;
}
public String gettelephone2() {
return this._telephone2;
}
public void settelephone2(String telephone2) {
this._telephone2=telephone2;
}
public String getDescription() {
return this._Description;
}
public void setDescription(String Description) {
this._Description=Description;
}
}
查看map.java文件
package lk.adspace.jaffnatemples;
import java.util.ArrayList;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
public class Viewmap extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;
private static final double SEATTLE_LAT = 9.663216, SEATTLE_LNG =80.01333;
private static final float DEFAULTZOOM = 15;
LocationClient mLocationClient;
ArrayList<Marker> markers = new ArrayList<Marker>();
static final int POLYGON_POINTS = 4;
Polygon shape;
Dbhandler dbhand = new Dbhandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.view_map);
if (initMap()) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
gotoLocation(SEATTLE_LAT, SEATTLE_LNG, DEFAULTZOOM);
MarkerOptions marker = new MarkerOptions().position(new LatLng(SEATTLE_LAT, SEATTLE_LNG)).title("Jaffna Clock Tower");
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
mMap.addMarker(marker);
ArrayList<kovil> object = dbhand.Get_Temple((getIntent().getExtras().getString("temple_type")), Integer.parseInt(getIntent().getExtras().getString("notemples")));
for (int i=0; i< Integer.parseInt(getIntent().getExtras().getString("notemples"));i++) {
kovil object2 = dbhand.Get_Temple(i);
String tempname = object2.gettemplename();
double latitude = Double.parseDouble(object2.getlatitude());
double longitude = Double.parseDouble(object2.getlongitude());
displaymarkers(latitude,longitude, tempname);
}
//
// displaymarkers(9.662502, 80.010239, "mugan kovil");
// displaymarkers(9.662502, 80.010239, "mugan kovil");
// displaymarkers(9.670931, 80.013201, "mugan kovil");
mMap.addPolygon(new PolygonOptions()
.add(new LatLng(9.662502, 80.010239), new LatLng(9.662502, 80.010239), new LatLng(9.670931, 80.013201), new LatLng(9.663216, 80.01333))
//.addHole(new LatLng(1, 1), new LatLng(1, 2), new LatLng(2, 2), new LatLng(2, 1), new LatLng(1, 1))
.fillColor(Color.GRAY));
int radius = Integer.parseInt(getIntent().getExtras().getString("distance"));
drawcircle(radius);
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
else {
setContentView(R.layout.main);
}
}
public void drawcircle(int rad){
int value = rad*1000;
Circle circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(9.663216, 80.01333))
.radius(value)
.strokeColor(Color.RED)
.fillColor(Color.WHITE));
}
public boolean displaymarkers(double lati, double longi, String templename){
MarkerOptions marker = new MarkerOptions().position(new LatLng(lati, longi)).title(templename);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon));
//marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mMap.addMarker(marker);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFrag.getMap();
}
return (mMap != null);
}
private void gotoLocation(double lat, double lng,
float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.moveCamera(update);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
}
错误堆栈
02-12 14:53:34.554: E/AndroidRuntime(979): FATAL EXCEPTION: main
02-12 14:53:34.554: E/AndroidRuntime(979): java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.adspace.jaffnatemples/lk.adspace.jaffnatemples.Viewmap}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.os.Handler.dispatchMessage(Handler.java:107)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.os.Looper.loop(Looper.java:194)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-12 14:53:34.554: E/AndroidRuntime(979): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 14:53:34.554: E/AndroidRuntime(979): at java.lang.reflect.Method.invoke(Method.java:525)
02-12 14:53:34.554: E/AndroidRuntime(979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-12 14:53:34.554: E/AndroidRuntime(979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-12 14:53:34.554: E/AndroidRuntime(979): at dalvik.system.NativeStart.main(Native Method)
02-12 14:53:34.554: E/AndroidRuntime(979): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-12 14:53:34.554: E/AndroidRuntime(979): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-12 14:53:34.554: E/AndroidRuntime(979): at lk.adspace.jaffnatemples.Dbhandler.Get_Temple(Dbhandler.java:114)
02-12 14:53:34.554: E/AndroidRuntime(979): at lk.adspace.jaffnatemples.Viewmap.onCreate(Viewmap.java:71)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.Activity.performCreate(Activity.java:5122)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
02-12 14:53:34.554: E/AndroidRuntime(979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-12 14:53:34.554: E/AndroidRuntime(979): ... 11 more
答案 0 :(得分:0)
Dbhandler dbhand = new Dbhandler(this);
ViewMap.java中的..应该位于onCreate()
中在DbHandler中,检查光标是否填充在:
kovil Kovil = new kovil(Integer.parseInt(cursor.getString(0)),
...
此外,我认为您应该添加/填充templelist变量
,而不是kovil答案 1 :(得分:0)
通过查看日志,您的问题似乎是您尝试获取列表中的第一个元素(索引0)并且您的列表为空(大小为0)
cursor.getString(0)
这似乎是个问题。你的光标没有记录