Android:无法访问其他类(服务)的功能

时间:2014-01-29 05:11:54

标签: java android eclipse sqlite

我是一名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 OR 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);
      }   

}

我没有看到任何教程中创建的任何runnables。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您将错误的参数类型传递给DBHelper构造函数。你必须这样做

DBHelper  dbh= new DBHelper(getApplicationContext());

void run() Runnable实例的mStatusChecker方法内的

。如果查看错误消息,您可以理解编译器无法为参数类型为DBHelper的{​​{1}}类找到合适的构造函数。这就是eclipse引发这个错误的原因。

希望你现在明白。