getSystemService(LAYOUT_INFLATER_SERVICE)显示错误

时间:2016-06-07 08:28:57

标签: android

getSystemService(LAYOUT_INFLATER_SERVICE)在以下代码中显示错误。能帮我解决这个问题。我有点陷入困境,如果没有它就不能继续前进,所以请帮我解决这个问题。

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.RatingBar;
    import android.widget.TextView;
    import navdrawerexample1.models.MovieModel;
    import java.util.List;

    public class MovieAdapter extends ArrayAdapter {

    private List<MovieModel> movieModelList;
    private int resource;
    private LayoutInflater inflater;
    public MovieAdapter(Context context, int resource, List<MovieModel> objects) {
        super(context, resource, objects);
        movieModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

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

        if (convertView == null){
            convertView=inflater.inflate(resource,null);
        }
        ImageView ivMovieIcon;
        TextView tvMovie;
        TextView tvTagline;
        TextView tvYear;
        TextView tvDuration;
        TextView tvDirector;
        RatingBar rbMovieRating;
        TextView tvCast;
        TextView tvStory;

        ivMovieIcon = (ImageView)convertView.findViewById(R.id.ivIcon);
        tvMovie = (TextView)convertView.findViewById(R.id.tvMovie);
        tvTagline = (TextView)convertView.findViewById(R.id.tvTagline);
        tvYear = (TextView)convertView.findViewById(R.id.tvYear);
        tvDuration = (TextView)convertView.findViewById(R.id.tvDuration);
        tvDirector = (TextView)convertView.findViewById(R.id.tvDirector);
        rbMovieRating = (RatingBar)convertView.findViewById(R.id.rbMovie);
        tvCast = (TextView)convertView.findViewById(R.id.tvCast);
        tvStory = (TextView)convertView.findViewById(R.id.tvStory);

        tvMovie.setText(movieModelList.get(position).getMovie());
        tvTagline.setText(movieModelList.get(position).getTagline());
        tvYear.setText("Year: " + movieModelList.get(position).getYear());
        tvDuration.setText("Duration:" + movieModelList.get(position).getDuration());
        tvDirector.setText("Director:" + movieModelList.get(position).getDirector());

        //rating bar

        rbMovieRating.setRating(movieModelList.get(position).getRating()/2);

        StringBuffer stringBuffer = new StringBuffer();
        for (MovieModel.cast cast : movieModelList.get(position).getCastList()){
            stringBuffer.append(cast.getName() + ", ");
        }
        tvCast.setText(stringBuffer);
        tvStory.setText("Cast:" + movieModelList.get(position).getStory());
        return convertView;
    }
}

4 个答案:

答案 0 :(得分:1)

更改此行:

inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 

到此:

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

答案 1 :(得分:0)

试一下

替换

 inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

   inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

答案 2 :(得分:0)

改变,

inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

注意:您需要在构造函数中传递上下文。

快乐编码..

答案 3 :(得分:0)

方法getSystemService属于Context类,因此您必须在上下文中运行它,但是您要从非Android类运行它。

因此,使用从活动传递的上下文访问getSystemService

inflater = ( LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);