Fragment尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

时间:2016-02-12 08:10:46

标签: android sqlite android-fragments

我正在创建一个在sqlite中检索数据的应用程序。首先,没有问题,但当我尝试在片段中传输它时,我无法检索数据,我得到一个错误。这是我第一次使用片段,所以我对我的错误没有任何想法。

public class Home_SpecialOffer extends Fragment {

    Context context;
    DatabaseHelper dbhelper;
    DatabaseHelper db = new DatabaseHelper(getActivity());

    ListView lvhome;
    List<TeaModel> GetHomeTea;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        dbhelper = new DatabaseHelper(getActivity());

        context = container.getContext();

        try{
            dbhelper.createDataBase();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        try {
            dbhelper.openDataBase();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        GetHomeTea = dbhelper.getHomeTea();
        lvhome = (ListView) container.findViewById(R.id.home_list);
        lvhome.setAdapter(new ViewAdapterHomeList());

        return inflater.inflate(R.layout.home_special,null);


    }

    public class ViewAdapterHomeList extends BaseAdapter {

        LayoutInflater mInflater;

        public ViewAdapterHomeList() {
            mInflater = LayoutInflater.from(context);
        }

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

        @Override
        public Object getItem(int position) {
            return null;
        }

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

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

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_home,null);
            }

            final TextView name = (TextView) convertView.findViewById(R.id.home_name);
            final TextView price = (TextView) convertView.findViewById(R.id.home_price);
            name.setText(GetHomeTea.get(position).getname());
            price.setText(GetHomeTea.get(position).getprice());

            Button btnbuy = (Button)convertView.findViewById(R.id.btnbuy);
            btnbuy.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),GetHomeTea.get(position).getname()+" IS NOT YET AVAILABLE!",
                            Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;
        }
    }
}

我收到此错误尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'并且它在此行中:lvhome.setAdapter(new ViewAdapterHomeList()) ;

1 个答案:

答案 0 :(得分:0)

你能试试这个吗?

您需要为片段布局 设置视图

 @Nullable
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);

        dbhelper = new DatabaseHelper(getActivity());

        context = container.getContext();

        try{
            dbhelper.createDataBase();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        try {
            dbhelper.openDataBase();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        GetHomeTea = dbhelper.getHomeTea();
        lvhome = (ListView) rootView.findViewById(R.id.home_list);
        lvhome.setAdapter(new ViewAdapterHomeList());

        return rootView;


    }

您需要传递 布局文件,而不是fragment_layout

希望这会对你有所帮助。