2016-02-12 95 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,歡迎:) –

相關問題