在Android中处理Fragment中的CustomAdapter

时间:2017-05-30 07:07:00

标签: java android android-fragments custom-adapter

这是片段

public class SoupFragment extends ListFragment{

    public static int [] subImages={R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin,R.drawable.tiffin};
    public static String [] subItmList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
    CustomAdapter customAdp;

    public SoupFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_soup, container, false);
        ListView lv = (ListView)view.findViewById(R.id.listView);

            customAdp = new CustomAdapter(SoupFragment.this,subItmList,subImages);

        //lv.setAdapter(new CustomAdapter(getActivity().getApplicationContext(),R.layout.list_row, subItmList,subImages));
        lv.setAdapter(customAdp);
        // Inflate the layout for this fragment
        return view;
    }

}

这是我的CustomAdapter

public class CustomAdapter extends  BaseAdapter{
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(SoupFragment soupFrag, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        imageId=prgmImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.list_row, null);
        holder.tv=(TextView) rowView.findViewById(R.id.txt_category);
        holder.img=(ImageView) rowView.findViewById(R.id.list_image);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });
        return rowView;
    }
}

我的问题是在运行时抛出错误

  

“尝试调用虚方法'java.lang.Object   在null上的android.content.Context.getSystemService(java.lang.String)'   对象参考“

。什么是确切的错误以及如何解决它。自己是新手android我不知道如何解决这个错误。

0 个答案:

没有答案