将listview放入片段时出错

时间:2015-03-12 08:21:48

标签: android listview android-fragments android-listview

每当我执行该应用程序崩溃时,我需要在片段中放置ListView

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

代码:

public class FindPeopleFragment extends Fragment {

    public FindPeopleFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         // Inflate the layout for this fragment
        //This layout contains your list view 
            View view = inflater.inflate(R.layout.tst, container, false);

           //now you must initialize your list view
           ListView listview =(ListView)view.findViewById(R.id.lvUsers);

           //EDITED Code 
           ArrayList<User> arrayOfUsers = User.getUsers();
           CustomUsersAdapter adapter = new CustomUsersAdapter(getActivity(), arrayOfUsers);


            listview.setAdapter(adapter);  

            //To have custom list view use this : you must define CustomeAdapter class
            // listview.setadapter(new CustomeAdapter(getActivity()));
           //getActivty is used instead of Context
         return view;
    }
}

public class CustomUsersAdapter extends ArrayAdapter<User> {
    public CustomUsersAdapter(Context context, ArrayList<User> users) {
        super(context, 0, users);
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
        User user = getItem(position);    
        if (convertView == null) {
           convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
        }
        TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
        TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
        tvName.setText(user.name);
        tvHome.setText(user.hometown);
        return convertView;
    }
}

public class User {
    public String name;
    public String hometown;

    public User(String name, String hometown) {
        this.name = name;
        this.hometown = hometown;
    }

    public static ArrayList<User> getUsers() {
        ArrayList<User> users = new ArrayList<User>();
        users.add(new User("Harry", "San Diego"));
        users.add(new User("Marla", "San Francisco"));
        users.add(new User("Sarah", "San Marco"));
        return users;
    }
}

如果我尝试使用简单的arrayadapter,它工作正常 工作代码

public class CommunityFragment extends Fragment {

    public CommunityFragment(){}

    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                Bundle savedInstanceState) {
                // Inflate the layout for this fragment
                //This layout contains your list view 
                    View view = inflater.inflate(R.layout.tst, container, false);

                   //now you must initialize your list view
                   ListView listview =(ListView)view.findViewById(R.id.list);

                   //EDITED Code 
                   String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
                   ArrayAdapter<String> adapter =
                   new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 

                    listview.setAdapter(adapter);  

                    //To have custom list view use this : you must define CustomeAdapter class
                    // listview.setadapter(new CustomeAdapter(getActivity()));
                   //getActivty is used instead of Context
                 return view;
            }
}

3 个答案:

答案 0 :(得分:0)

检查一下:

     @Override
 public View getView(int position, View convertView, ViewGroup parent) {
    User user = getItem(position);    
    if (convertView == null) {
   LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
    }
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
    TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
    tvName.setText(user.name);
    tvHome.setText(user.hometown);
    return convertView;
}

答案 1 :(得分:0)

您需要在List中添加值。 如果要直接向List添加值,请尝试此操作。

arrayOfUsers.add(Variable/Value);

insted of

ArrayList<User> arrayOfUsers = User.getUsers();

OR

如果你想根据你的代码添加其他类的值“用户”是类,所以你需要将该类的对象添加到List

User obj = new User(parameters if any);
arrayOfUsers.add(obj);

答案 2 :(得分:0)

原因是您在构造函数中使用了错误的layoutResourceId,将代码修改为:

public class CustomUsersAdapter extends ArrayAdapter<User> {

   private Context context;
   private ArrayList<User> users;
   private int layoutId;

   public CustomUsersAdapter(Context context, int layoutResourceId, ArrayList<User> users) {

      super(context, layoutResourceId, users);

      this.context = context;
      this.users = users;
      this.layoutId = layoutResourceId;
   }

 @Override
 public int getCount()
 {

    return users.size();
 }


 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
    User user = getItem(position);    
    if (convertView == null) {

       //use 'context' here
       convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
    }
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
    TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
    tvName.setText(user.name);
    tvHome.setText(user.hometown);
    return convertView;
}

}

此外,请将FindPeopleFragment代码中的适配器调用修改为:

ArrayList<User> arrayOfUsers = User.getUsers();
       CustomUsersAdapter adapter = new CustomUsersAdapter(getActivity(), R.layout.item_user, arrayOfUsers);

希望这有帮助!