gpt4 book ai didi

java - 安卓 : Unable to Access Function From Other Class (Service)

转载 作者:行者123 更新时间:2023-12-01 23:14:47 26 4
gpt4 key购买 nike

我是一名 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);//我什至无法在此处的建议中获取 insert_records 函数 OR DBHelper dbh= new DBHelper(this); dbh.insert_records("12.2323", "25.22222"); 可爱的 Eclipse 抛出错误消息构造函数 DBHelper(new Runnable(){}) 未定义。我根本不知道如何访问我的函数。

我有

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());

mStatusChecker Runnable 实例的 void run() 方法中。如果您查看错误消息,您可以了解到编译器无法为参数类型为 Runnable 的 DBHelper 类找到合适的构造函数。这就是 eclipse 抛出此错误的原因。

希望你现在明白了。

关于java - 安卓 : Unable to Access Function From Other Class (Service),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com