BroadCastReceiver类中未定义构造函数

时间:2015-09-01 23:47:29

标签: java android

当我从服务器收到gcm消息时,我正在尝试将数据从远程服务器插入本地数据库。 我创建了一个数据库控制器类和broadcastreceiver类。

数据库控制器类

public class DBController  extends SQLiteOpenHelper {

    public DBController(Context applicationcontext) {
        super(applicationcontext, "user.db", null, 1);
    }
    //Creates Table
    @Override
    public void onCreate(SQLiteDatabase database) {
        String query;
        query = "CREATE TABLE users ( userId INTEGER, userName TEXT)";
        database.execSQL(query);
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS users";
        database.execSQL(query);
        onCreate(database);
    }

    /**
     * Inserts User into SQLite DB
     * @param queryValues
     */
    public void insertUser(HashMap<String, String> queryValues) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", queryValues.get("userId"));
        values.put("userName", queryValues.get("userName"));
        database.insert("users", null, values);
        database.close();
    }

    /**
     * Get list of Users from SQLite DB as Array List
     * @return
     */
    public ArrayList<HashMap<String, String>> getAllUsers() {
        ArrayList<HashMap<String, String>> usersList;
        usersList = new ArrayList<HashMap<String, String>>();
        String selectQuery = "SELECT  * FROM users";
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("userId", cursor.getString(0));
                map.put("userName", cursor.getString(1));
                usersList.add(map);
            } while (cursor.moveToNext());
        }
        database.close();
        return usersList;
    }

}

GCMBroadcastReceiver.java

public class GCMBroadcastReceiver extends BroadcastReceiver {
    DBController controller = new DBController(this);
    HashMap<String, String> queryValues; 

     @Override
        public void onReceive(Context context, Intent intent) {
             ...
        }
}

现在问题是当我在GCMBroadcastReceiver.java类中调用我的数据库控制器类(DBController.java)时,它会在下面给出一条错误消息。请问我有什么问题吗?

  

描述资源路径位置类型   构造函数DBController(GCMBroadcastReceiver)未定义GCMBroadcastReceiver.java / GCMtest / src / com / gcm / gcmtest第14行Java问题

3 个答案:

答案 0 :(得分:0)

new DBController(this)中,您有一行BroadcastReceiverContext不是Context,因此没有有效的构造函数。使用传递给onReceive()的{​​{1}}。这意味着将该字段的初始化推迟到onReceive()

答案 1 :(得分:0)

在GCMBroadcastReceiver类中,您初始化DBController(this)。尝试在onReceive()方法中初始化DBController。您可以使用method参数中的上下文,如下所示:controller = new DBController(context);

答案 2 :(得分:0)

在onReceive()方法中初始化你的DBController,因为上下文将传递给它,你可以修改该上下文来初始化你的DBController。

对于前。喜欢这个..

public class GCMBroadcastReceiver extends BroadcastReceiver {
DBController controller ;
HashMap<String, String> queryValues; 

 @Override
    public void onReceive(Context context, Intent intent) {
         controller =new DBController(context);
    }

}

我希望这会对你有所帮助。

初始化后,您可以在此课程的任何位置使用其实例。