有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:无法从其他类(服务)访问函数

我是一名php开发人员,正在学习安卓开发方法。 我有一个dbhelper类,它的代码如下

package com.example.bootstart;

import 安卓.content.Context;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.database.sqlite.SQLiteOpenHelper;
import 安卓.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 hereDBHelper dbh= new DBHelper(this); dbh.insert_records("12.2323", "25.22222");可爱的eclipse抛出错误消息The constructor DBHelper(new Runnable(){}) is undefined。 我根本不知道如何访问我的功能

我有

SQLiteOpenHelper dbhelper;
    SQLiteDatabase database;

定义在我试图访问的类的顶部

下面是我试图从中访问dbhelper函数的类

package com.example.bootstart;


import 安卓.app.Service;
import 安卓.content.Intent;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.database.sqlite.SQLiteOpenHelper;
import 安卓.os.Handler;
import 安卓.os.IBinder;
import 安卓.util.Log;
import 安卓.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);
      }   

}

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


共 (1) 个答案

  1. # 1 楼答案

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

    DBHelper  dbh= new DBHelper(getApplicationContext());
    

    mStatusChecker可运行实例的void run()方法中。如果查看错误消息,可以理解编译器无法为参数类型为RunnableDBHelper类找到合适的构造函数。这就是为什么eclipse会抛出这个错误

    希望你现在明白了