public class Database extends SQLiteOpenHelper {
private static final String DB_NAME = "mydatabase";
private static final String TABLE_MEMBER = "schedule1";
private static final int DB_VERSION = 1;
SQLiteDatabase db;
public Database(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_MEMBER + "("
+ "idk INTEGER primary key AUTOINCREMENT,"
+ "id TEXT(100),"
+ "name TEXT(100),"
+ "address TEXT(100),"
+ "latitude INTEGER,"
+ "longtitude INTEGER);");
Log.d("CREATE TABLE","Create Table Successfully.");
}
public void insertdb(String id1,String name1,String address1,String latitude1,String longtitude1){
try{
db = this.getWritableDatabase(); // Write Data
db.execSQL("INSERT or replace INTO "+TABLE_MEMBER+" (idk,id,name,address,latitude,longtitude) " +
"VALUES(NULL,'"+id1+"','"+name1+"','"+address1+"','"+latitude1+"','"+longtitude1+"')");
db.close();
}catch(Exception e){e.printStackTrace();}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE "+TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}
public Cursor selectdata(){
try{
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT * FROM " + TABLE_MEMBER;
Cursor cursor = db.rawQuery(strSQL, null);
return cursor;
}catch(Exception e){return null;}
}
}
และหน้าที่ 3 ที่มีปัญหานะครับ Code (Android-Java)
public class Googlemap extends FragmentActivity {
private GoogleMap myMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_googlemap);
SupportMapFragment mySupportMapFragment =
(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map_canvas);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
Database daba = new Database(this);
Cursor locationCursor = daba.selectdata();
int i=0;
do {
String id = locationCursor.getString(locationCursor
.getColumnIndex("id"));
String name = locationCursor.getString(locationCursor
.getColumnIndex("name"));
Double latitude = (Double) locationCursor.getDouble(locationCursor
.getColumnIndex("latitude"));
Double longitude = (Double) locationCursor.getDouble(locationCursor
.getColumnIndex("longtitude"));
Log.i("Recieve"+i,idowner+" "+latitude);
i++;
// try{
// myMap.addMarker(new MarkerOptions()
// .position(new LatLng(latitude, longitude))
// .title(id)
// .snippet(name)
// .icon(BitmapDescriptorFactory.defaultMarker()));
// }catch(Exception e){e.printStackTrace();}
}while(locationCursor.moveToNext());
}
}
Code
04-19 18:10:05.734: E/AndroidRuntime(4775): FATAL EXCEPTION: main
04-19 18:10:05.734: E/AndroidRuntime(4775): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.police/com.example.police.Googlemap}; have you declared this activity in your AndroidManifest.xml?
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.app.Activity.startActivityForResult(Activity.java:3190)
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.app.Activity.startActivity(Activity.java:3297)
04-19 18:10:05.734: E/AndroidRuntime(4775): at com.example.police.DetailActivity$1$1.onClick(DetailActivity.java:123)
04-19 18:10:05.734: E/AndroidRuntime(4775): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.os.Looper.loop(Looper.java:137)
04-19 18:10:05.734: E/AndroidRuntime(4775): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-19 18:10:05.734: E/AndroidRuntime(4775): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:10:05.734: E/AndroidRuntime(4775): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 18:10:05.734: E/AndroidRuntime(4775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-19 18:10:05.734: E/AndroidRuntime(4775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-19 18:10:05.734: E/AndroidRuntime(4775): at dalvik.system.NativeStart.main(Native Method)
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.example.db.Dbselect;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class Googlemap extends FragmentActivity {
private GoogleMap map;
private Dbselect db;
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_googlemap);
SupportMapFragment mySupportMapFragment =
(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map_canvas);
map = mySupportMapFragment.getMap();
context = this;
db = new Dbselect(context);
Cursor locationCursor = db.selectdata();
int i=0;
try{
do {
String id = locationCursor.getString(locationCursor
.getColumnIndex("id"));
String name = locationCursor.getString(locationCursor
.getColumnIndex("name"));
Double latitude = (Double) locationCursor.getDouble(locationCursor
.getColumnIndex("latitude"));
Double longitude = (Double) locationCursor.getDouble(locationCursor
.getColumnIndex("longtitude"));
Log.i("test"+i,idowner+" "+latitude);
i++;
}while(locationCursor.moveToNext());
}catch(Exception e){e.printStackTrace();}
}
}
พอใส่ debug แล้วเป็นแบบนี้ครับ Code
04-20 17:48:35.668: W/System.err(2720): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
04-20 17:48:35.676: W/System.err(2720): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
04-20 17:48:35.696: W/System.err(2720): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-20 17:48:35.696: W/System.err(2720): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
04-20 17:48:35.706: W/System.err(2720): at com.example.police1.Googlemap.onCreate(Googlemap.java:37)
04-20 17:48:35.706: W/System.err(2720): at android.app.Activity.performCreate(Activity.java:4465)
04-20 17:48:35.706: W/System.err(2720): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-20 17:48:35.716: W/System.err(2720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-20 17:48:35.716: W/System.err(2720): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-20 17:48:35.716: W/System.err(2720): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-20 17:48:35.726: W/System.err(2720): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-20 17:48:35.726: W/System.err(2720): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 17:48:35.726: W/System.err(2720): at android.os.Looper.loop(Looper.java:137)
04-20 17:48:35.726: W/System.err(2720): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-20 17:48:35.736: W/System.err(2720): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 17:48:35.754: W/System.err(2720): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 17:48:35.756: W/System.err(2720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-20 17:48:35.756: W/System.err(2720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-20 17:48:35.756: W/System.err(2720): at dalvik.system.NativeStart.main(Native Method)