当前位置: 首页 > 知识库问答 >
问题:

无法使用volley库实例化片段中的列表

刘狐若
2023-03-14

我的问题是,当我把这段代码放在一个活动中时,它工作得很好,但当我试图使用片段时,它会生成错误这就是显示的错误:

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<News> newItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<News> newItems) {
    this.activity = activity;
    this.newItems = newItems;
}

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

@Override
public Object getItem(int location) {
    return newItems.get(location);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView title = (TextView) convertView.findViewById(R.id.title);

    // getting new data for the row
    News m = newItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getImageURL(), imageLoader);

    // title
    title.setText(m.getTitle());

    return convertView;
}
public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

24号线是:

ImageLoader imageLoader = AppController.getInstance().getImageLoader();

共有1个答案

宋耀
2023-03-14

我认为您忘记在清单中添加应用程序实现,如下所示

    <application
    android:name=".package.AppController "
    ...
    />

这就是GetInstance返回NULL的原因。

 类似资料: