简体   繁体   中英

Android - GridView with a custom adapter doesn't work

I've created a Custom Adapter for ListView and GridView. It's all the same, I just want one tab with List, other with GridView. I can set the data to ListView on Fragment1 but I can not do the same for GridView on Fragment2 (Gives a Null Pointer Exception).

Codes are below :

Fragment2.java :

public class FragmentTab2 extends Fragment {
    GridView gridView;
    Intent intent;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
        gridView = (GridView) rootView.findViewById(R.id.GridView1);
        return rootView;
    }
    public GridView getGridView() {
        return gridView;
    }
}

CustomAdapter.java :

public class CustomAdapter extends BaseAdapter {
    private ArrayList<ListData> data;
    private Context context;

    CustomAdapter(Context context, ArrayList<ListData> data) {
        this.context = context;
        this.data = data;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = convertView;

        if(rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item, null);
        }

        ImageView image = (ImageView) rowView.findViewById(R.id.listLogo);
        TextView nameVideo = (TextView) rowView.findViewById(R.id.label);
        TextView link = (TextView) rowView.findViewById(R.id.link);

        // Get individual object from  ArrayList<ListData> and set ListView items
        ListData temp_data = data.get(position);
        image.setImageBitmap(temp_data.getImage());
        nameVideo.setText(temp_data.getName());
        link.setText(temp_data.getLink());

        return rowView;
    }
}

And the main

ListView lw = fragmentTab1.getListView();
        lw.setAdapter(new CustomAdapter(this, dataArray));
        GridView gr = fragmentTab2.getGridView();
        //gr.setAdapter(new CustomAdapter(this, dataArray));

The commented part is giving the Null Exception, everything else is fine. Any ideas? Thanks.

android.app.FragmentManager fm = getFragmentManager();
        android.app.FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, fragmentTab2);
        ft.addToBackStack(null);
        ft.commit();

It looks like fragmentTab2.getGridView(); is returning null.

Make sure the GridView in your fragmenttab2 actually has the id GridView1 .

我认为dataArray的问题可能是它的第一个适配器消耗的,尝试在网格中设置单独的数组并检查

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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