繁体   English   中英

Android:调用getApplicationContext()时出现上下文错误

[英]Android: context error when calling getApplicationContext()

我有一个数据库,用于存储一些照片的名称。 在onCreate()方法中,我创建了一个新的PointofInterestAdapter:

String[] from=new String[] {"name", "address", "favorite", "type", "distance"};
int[] to = new int[] {R.id.name, R.id.address, R.id.favoriteImage, R.id.icon, R.id.distance};
//SCAdapter = new SimpleCursorAdapter(this, R.layout.row, null, from ,to, 0);   
SCAdapter = new PointOfInterestAdapter(this, R.layout.row_subcategory, null, from ,to, 0);
list.setAdapter(SCAdapter);

这是制作SCAdapter的代码:

class PointOfInterestAdapter extends SimpleCursorAdapter {

    PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(ctxt,layout,c,from,to,flags);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
      LayoutInflater inflater=getLayoutInflater();
      View row=inflater.inflate(R.layout.row_subcategory, parent, false);
      PointOfInterestHolder holder=new PointOfInterestHolder(row);
      row.setTag(holder);
      return row;
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        PointOfInterestHolder holder=(PointOfInterestHolder)row.getTag();
        holder.populateFrom(c, databaseConnector);
    }
}

static class PointOfInterestHolder {
    private TextView name=null;
    private TextView address=null;
    private ImageView icon=null;
    private ImageView favoriteImage=null;
    private TextView distance=null;

    PointOfInterestHolder(View row) {
        name=(TextView)row.findViewById(R.id.name);
        address=(TextView)row.findViewById(R.id.address);
        favoriteImage=(ImageView)row.findViewById(R.id.favoriteImage);
        icon=(ImageView)row.findViewById(R.id.icon);
        distance=(TextView)row.findViewById(R.id.distance);
    }

    void populateFrom(Cursor c, DatabaseConnector databaseConnector) {
        name.setText(databaseConnector.getName(c));
        address.setText(databaseConnector.getAddress(c));
        distance.setText(databaseConnector.getDistance(c)+" m");

        //---set the image ---
        String photo_name=c.getString(c.getColumnIndex("photo_name"));
        int resID = getApplicationContext().getResources().getIdentifier(photo_name, "drawable",  getApplicationContext().getPackageName());
        //Resources res = getResources();
        //Drawable drawable=res.getDrawable(R.drawable.myimage);
        icon.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));


        //--> set favorite image
        if(databaseConnector.getFavorite(c).equals("yes")) {
            favoriteImage.setImageResource(R.drawable.favorite_yes);
        }
        else if (databaseConnector.getFavorite(c).equals("no")) {
            favoriteImage.setImageResource(R.drawable.favorite_no);
        }
    }
}

在populateForm(Cursor c,DatabaseConnector databaseConnector)我尝试设置图像

问题是我收到该错误消息:

“无法从ContextWrapper类型静态引用非静态方法getApplicationContext()”

行:

int resID = getApplicationContext()。getResources()。getIdentifier(photo_name,“ drawable”,getApplicationContext()。getPackageName());

和这里:

icon.setImageDrawable(getApplicationContext()。getResources()。getDrawable(resID));

我该如何解决这个问题? 先感谢您。

作为@ Luksprog,您将需要活动上下文,该上下文将传递给适配器类的构造函数。

你有这个

  private Context mContext; 
  PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) {
    super(ctxt,layout,c,from,to,flags);
    mContext = ctxt;
}

然后使用上下文

 int resID = mContext.getResources().getIdentifier(photo_name, "drawable", mContext.getPackageName());

注意

不要保留对上下文活动的长期引用(对活动的引用应与活动本身具有相同的生命周期)

更多信息 @

http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html

这是您定义的构造函数。 我看到您将其中的上下文作为参数传递。 本身使用PointOfInterestAdapter(上下文ctxt,int布局,光标c,String []从,int []到int标志){super(ctxt,layout,c,from,to,flags); }

您在构造函数中已经有ctxt / Context,请声明一个本地变量并将其存储在其中。 像下面

class PointOfInterestAdapter extends SimpleCursorAdapter {

Static Context mCtx; // local context instance
    PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(ctxt,layout,c,from,to,flags);
    mCtx = ctxt; // assigning context instance in local variable
    }

......................

现在在任何使用getApplicationContext()地方都使用mCtx

暂无
暂无

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

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