2016-02-12 139 views
1

我创建了一个应用程序来检索sqlite中的数据。首先,没有问题,但是当我尝试在片段中传输它时,我无法检索数据,并且出现错误。这是我第一次使用片段,所以我对我的错误没有任何想法。片段尝试调用空对象引用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'参考

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; 
     } 
    } 
} 

我得到这个错误尝试上的空对象引用调用虚拟方法“无效android.widget.ListView.setAdapter(android.widget.ListAdapter)”,它是在这条线:lvhome.setAdapter(新ViewAdapterHomeList());

回答

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

希望这会帮助你。

+0

它的工作!谢谢你,先生:)我会接受你的答案,因为stackoverlow说我不能接受你的答案在8分钟。 –

+0

@ A.Mallavo,欢迎:) –

相关问题