无法解析上下文方法

时间:2015-06-17 06:56:04

标签: java android database

我试图在我的应用程序中使用数据库,并使用onCreate和onUpgrade方法以及构造函数创建了一个帮助程序类。然后在另一个类中,我尝试使用getContext()创建一个helper类的对象,就像在android开发文档中一样。但我得到一个错误说"无法解析方法getContext()"。我google了,尝试像getApplicationContext,但是没有用。论证中的这一点也不起作用。这是我尝试编写的类:

public class SearchResultItem {

DbHelper dbHelper = new DbHelper(getContext());

@JsonKey("Title") // You need to write this so the jsonparser knows which value to get
private String mTitle;

@JsonKey("Year")
private String mYear;

@JsonKey("imdbID")
private String mImdbId;

@JsonKey("Type")
private String mType;

public String getTitle()
{
    return mTitle;
}

public String getYear()
{
    return mYear;
}

public String getImdbId()
{
    return mImdbId;
}

public String getType()
{
    return mType;
}

}

如果需要,我的DbHelper课程就像这样:

public class DbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "movies.db";

//Table name
private static final String MOVIE_TABLE = "movies";

//Column names
private static final String COLUMN_ID = "id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_YEAR = "year";
private static final String COLUMN_PLOT = "plot";

Context context;
public DbHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) { // Called when the database is created for the first time
    final String SQL_MOVIE_TABLE = "CREATE TABLE IF NOT EXISTS " + MOVIE_TABLE + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TITLE + " TEXT NOT NULL," +
            COLUMN_YEAR + " REAL," + COLUMN_PLOT + " TEXT" + ")";
    sqLiteDatabase.execSQL(SQL_MOVIE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { // Called when the database needs to be upgraded/changed or dropped
    //Drop older table if it exists
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + MOVIE_TABLE);

    //Create table again
    onCreate(sqLiteDatabase);
}

private boolean haveNetworkConnection()
{
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for(NetworkInfo ni : netInfo)
    {
        if(ni.getTypeName().equalsIgnoreCase("WIFI"))
        {
            if(ni.isConnected())
            {
                haveConnectedWifi = true;
            }
        }

        if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
        {
            if(ni.isConnected())
            {
                haveConnectedMobile = true;
            }
        }
    }

    return haveConnectedWifi || haveConnectedMobile;
}

任何人都有任何线索可以解决这个问题吗?

1 个答案:

答案 0 :(得分:-2)

试试这个

  context = getApplicationContext();
super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

希望有所帮助