简体   繁体   English

在BroadCastReceiver类中未定义的构造方法

[英]Constructor undefined in a BroadCastReceiver class

I am trying to insert data from my remote server into my local database when a gcm message is received from my servers. 从服务器收到gcm消息时,我试图将数据从远程服务器插入本地数据库。 I have created a database controller class and broadcastreceiver class. 我创建了一个数据库控制器类和broadcastreceiver类。

Database Controller class 数据库控制器类

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 GCMBroadcastReceiver.java

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

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

Now the problem is when my Database Controller class (DBController.java) is called in the GCMBroadcastReceiver.java class it gives an error message below. 现在的问题是,当在GCMBroadcastReceiver.java类中调用我的数据库控制器类(DBController.java)时,它在下面给出了一条错误消息。 Please is there something i am doing wrong? 请问我做错了什么吗?

Description Resource Path Location Type The constructor DBController(GCMBroadcastReceiver) is undefined GCMBroadcastReceiver.java /GCMtest/src/com/gcm/gcmtest line 14 Java Problem 说明资源路径位置类型构造函数DBController(GCMBroadcastReceiver)未定义GCMBroadcastReceiver.java / GCMtest / src / com / gcm / gcmtest第14行Java问题

In GCMBroadcastReceiver , you have a line with new DBController(this) in it. GCMBroadcastReceiver ,您可以在其中一行包含new DBController(this) A BroadcastReceiver is not a Context , and so there is no valid constructor. BroadcastReceiver不是Context ,因此没有有效的构造函数。 Use the Context passed into onReceive() . 使用传递给onReceive()Context This will mean delaying initializing that field until onReceive() . 这将意味着延迟初始化该字段,直到onReceive()为止。

In your GCMBroadcastReceiver class, you initialize DBController(this) . 在您的GCMBroadcastReceiver类中,初始化DBController(this) Try initializing your DBController inside of your onReceive() method. 尝试在onReceive()方法内部初始化DBController。 You can use the context from the method parameter so it looks like this: controller = new DBController(context); 您可以使用method参数中的上下文,使其看起来像这样: controller = new DBController(context);

Initialize your DBController in onReceive() method because the context will be passed to it and you can nuse that context to initialize your DBController. 使用onReceive()方法初始化DBController,因为上下文将传递给它,并且您可以使用该上下文来初始化DBController。

For ex. 对于前。 like this.. 像这样..

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

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

} }

I hope this would help you. 希望对您有帮助。

Once it is initialized you can use its instance anywhere in this class. 一旦初始化,就可以在此类的任何地方使用其实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM