2014-01-29 47 views
0

我是一名PHP开发人员,正在学习围绕android开发的方式。 我有一个dbhelper助手类,它具有下面的代码。Android:无法从其他类(服务)访问函数

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


    } 
} 

然而,当我尝试使用

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");可爱的日食从另一个类访问insert_records函数抛出错误消息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); 
     } 

}

我还没有看到任何教程创建任何可运行。 我们非常感谢您的帮助。

回答

3

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

DBHelper dbh= new DBHelper(getApplicationContext()); 

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

希望你现在明白。

+0

我的我的。我似乎不了解基础知识。猜猜我不得不回到其他一些基本教程来理解这一点。非常感谢你的帮助。 –

+0

非常欢迎您,请接受让别人知道它工作并关闭的答案。谢谢! – Keerthivasan

+0

绝对。 SFO要我留4分钟。 –