不是封闭的 class 错误 (Android)

Not an enclosing class Error (Android)

在下面的一段代码中,我在自定义适配器中尝试传递 DisplayWishesActivity.this 时遇到错误。 DisplayWishesActivity 是另一个 activity,尚未修改。 (错误行请参考代码)

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import data.DatabaseHandler;
import model.MyWish;


public class WishDetailActivity extends ActionBarActivity {
    private DatabaseHandler databaseHandler;
    private ArrayList<MyWish> wishList;
    private WishAdapter wishAdapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wish_detail);

        wishList = new ArrayList<>();
        listView = (ListView) findViewById(R.id.list_id);

        refreshData();
    }

    private void refreshData(){
        wishList.clear();

        databaseHandler = new DatabaseHandler(getApplicationContext());

        ArrayList<MyWish> wishesFromDB = databaseHandler.getWishes();

        for(MyWish wish : wishesFromDB){
            String title = wish.getTitle();
            String date = wish.getRecordDate();
            String content = wish.getContent();

            MyWish myWish = new MyWish();

            myWish.setTitle(title);
            myWish.setContent(content);
            myWish.setRecordDate(date);

            wishList.add(myWish);

        }
        databaseHandler.close();

        //setup adapter

//*******ERROR********* -->>"DisplayWishesActivity is not an enclosing class"            

        wishAdapter = new WishAdapter(DisplayWishesActivity.this, R.layout.wish_row, wishList);  
    }


    public class WishAdapter extends ArrayAdapter<MyWish>{          //Private Class
        Activity activity;
        int layoutResource;
        MyWish wish;
        ArrayList<MyWish> mData = new ArrayList<>();

        public WishAdapter(Activity act, int resource, ArrayList<MyWish> data) {
            super(act, resource, data);

            activity = act;
            layoutResource = resource;
            mData = data;

            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public MyWish getItem(int position) {
            return mData.get(position);
        }

        @Override
        public int getPosition(MyWish item) {
            return super.getPosition(item);
        }

        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }


        //This method is the heart of our custom adapter class, as this is the place where our custom ListView is created

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder;

            if(row == null || row.getTag() == null){              // i.e. the our list view (row) is currently empty
                LayoutInflater inflater = LayoutInflater.from(activity);
                row = inflater.inflate(layoutResource, null);
                holder = new ViewHolder();

                holder.mTitle = (TextView) row.findViewById(R.id.wish_title_in_list_id);
                holder.mDate = (TextView) row.findViewById(R.id.date_in_list_id);

                row.setTag(holder);
            }

            else{
                holder = (ViewHolder) row.getTag();         //prevents the system from doing all the work again, but instead recycles the views
            }

            holder.myWish = getItem(position);

            holder.mTitle.setText(holder.myWish.getTitle());
            holder.mDate.setText(holder.myWish.getRecordDate());

            return row;
        }
    }


    class ViewHolder{              //helps in recycling the listView, rather than instantiating it every time
        MyWish myWish;
        TextView mTitle;                         
        TextView mId;
        TextView mContent;
        TextView mDate;
    }

}

我不知道是什么让你这样做,但这是错误的。语法:

ClassName.this

只能在ClassNameclass中使用。在您的情况下,当您尝试使用

DisplayWishesActivity.this

它给出一个错误,因为你不在 DisplayWishesActivity class 中。您需要 DisplayWishesActivity class 的实例来调用 WishAdapter class 的构造函数。尝试创建一个 DisplayWishesActivity class 的实例,或者只是找到一个 class 的实例来替换

DisplayWishesActivity.this

这应该可以解决问题。