无法解析的上下文方法 [英] Cannot resolve context method

查看:1354
本文介绍了无法解析的上下文方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序使用的数据库,并创建了的onCreate和onUpgrade方法和构造一个辅助类。然后在另一个类中我尝试创建的getContext(),因为他们做对Android开发文档帮助我的类的对象。但我得到一个错误说无法解析法的getContext()。我用Google搜索,并尝试像getApplicationContext并不起作用。这在参数也不起作用。这里是我试着写一个类:

I'm trying to use database in my app and have created an helper class with onCreate and onUpgrade method and an constructor. Then in another class I try to create an object of my helper class with getContext() as they do on the android development documentation. But I get an error saying "cannot resolve method getContext()". I have googled and try like getApplicationContext and that doesn't work. This in the argument doesn't work either. Here is the class I try to write it:

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类看起来是这样的,如果它的需要:

And my DbHelper class looks like this if it's needed:

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;
}

任何人有我如何能解决这个任何线索?

Anyone have any clues on how I can solve this?

推荐答案

试试这个

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

}

希望它能帮助

这篇关于无法解析的上下文方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆