我是一名PHP开发人员,学习关于Android开发的方法。
我有一个dbhelper辅助类,其中包含以下代码。
package com.example.bootstart;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
private static final String LOGTAG = "THEDBHELPER";
private static final String DATABASE_NAME = "geolocation.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_LOCATIONS = "locations";
private static final String COLUMN_ID = "id";
private static final String COLUMN_welawa = "welawa";
private static final String COLUMN_lati = "latitude";
private static final String COLUMN_longi = "longitude";
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_LOCATIONS + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_welawa + " TEXT, " + COLUMN_lati
+ " TEXT, " + COLUMN_longi + " TEXT)";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(LOGTAG, "TABLE HAS BEEN CREATED");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS);
onCreate(db);
}
public void insert_records(String lati, String longi) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
sqLiteDatabase.execSQL("INSERT INTO " +
TABLE_LOCATIONS +
"(welawa,latitude,longitude) Values (" + ts + ","+ lati +"," + longi + ");");
Log.i(LOGTAG, "Data Entered");
}
}
但是,当我尝试通过使用另一个类访问insert_records函数时
dbhelper = new DBHelper(this); //I can't even get the insert_records function in the suggestions in the drop here
或DBHelper dbh= new DBHelper(this); dbh.insert_records("12.2323", "25.22222");
可爱的日食会引发错误消息The constructor DBHelper(new Runnable(){}) is undefined
。我根本不知道如何访问我的功能。
我有
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
定义在我尝试访问的类的顶部。
下面是试图从中访问dbhelper函数的类。
package com.example.bootstart;
import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class GPSService extends Service {
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
GPSTracker gps;
private int mInterval = 10000;
private Handler mHandler;
@Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "StartAtBootService Created");
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
//Service Will Start until stopped
mHandler = new Handler();
Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
mStatusChecker.run();
return START_STICKY;
}
public void onDestroy(){
super.onDestroy();
Toast.makeText(this,"Service Stopped",Toast.LENGTH_LONG).show();
}
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
gps = new GPSTracker(GPSService.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
//new AsyncSendData().execute("http://www.accuretta.lk/nv/maps/track_post"); Sending Info Code Is Working Fine
DBHelper dbh= new DBHelper(this);
dbh.insert_records("12.2323", "25.22222");
dbh.insert_records("55.2222", "11.256");
}
mHandler.postDelayed(mStatusChecker, mInterval);
}
};
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
}
我没有在任何教程中看到任何可运行的对象。
非常感谢您的帮助。
最佳答案
您将错误的参数类型传递给DBHelper
构造函数。你必须这样
DBHelper dbh= new DBHelper(getApplicationContext());
在
void run()
Runnable实例的mStatusChecker
方法中。如果查看错误消息,可以理解编译器无法为参数类型为DBHelper
的Runnable
类找到合适的构造函数。这就是为什么Eclipse会引发此错误的原因。希望你现在明白了。