使用Fragment Android将自定义适配器设置为listview时获取Null Pointer Exception

时间:2015-03-16 15:17:31

标签: android listview

在将自定义适配器设置为listview时,我收到了Null Pointer Exception。 请注意我使用片段

我正在创建一个具有导航器抽屉的应用程序。 Slider菜单的每一项都引导我们一个片段,其中一个片段有一个listview 这是我的 SocietyFragmentCode:

private List<Movie> movieList = new ArrayList<Movie>();
private static final String TAG = MainActivity.class.getSimpleName();
private CustomListAdapter adapter;
private static final String url = "http://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private ListView listView;


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

    View rootView = inflater.inflate(R.layout.activity_society_fragment, container, false);






    listView = (ListView)getActivity().findViewById(R.id.list_societe);
    adapter = new CustomListAdapter(getActivity().getBaseContext(),0, movieList);
    listView.setAdapter(adapter);

    return rootView;


}

CustomListAdapter.Java代码:

   public class CustomListAdapter extends ArrayAdapter<Movie> {


private Context context;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();


public CustomListAdapter(Context context, int resource, List<Movie> movieItems) {
    super(context, resource, movieItems);
    this.context = context;
    this.movieItems = movieItems;
}


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

@Override
public Movie getItem(int location) {
    return movieItems.get(location);
}

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

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

    if (inflater == null)
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row,null);
    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView title = (TextView) convertView.findViewById(R.id.title);
    TextView rating = (TextView) convertView.findViewById(R.id.rating);
    TextView genre = (TextView) convertView.findViewById(R.id.genre);
    TextView year = (TextView) convertView.findViewById(R.id.releaseYear);

    // getting movie data for the row
    Movie m = movieItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

    // title
    title.setText(m.getTitle());

    // rating
    rating.setText("Rating: " + String.valueOf(m.getRating()));

    // genre
    String genreStr = "";
    for (String str : m.getGenre()) {
        genreStr += str + ", ";
    }
    genreStr = genreStr.length() > 0 ? genreStr.substring(0,
            genreStr.length() - 2) : genreStr;
    genre.setText(genreStr);

    // release year
    year.setText(String.valueOf(m.getYear()));

    return convertView;
}

}

和HomeActivity.java:

    mTitle = mDrawerTitle = getTitle();
    mMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(android.R.id.list);   
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

    List<Iitem> items = new ArrayList<Iitem>();


    items.add(new Header("Sec1"));
    items.add(new ListItem(mMenuTitles[0],navMenuIcons.getResourceId(0, -1)));
    items.add(new ListItem(mMenuTitles[1],navMenuIcons.getResourceId(1, -1),"29",true));
    items.add(new Header("sec2"));
    items.add(new ListItem(mMenuTitles[2], navMenuIcons.getResourceId(2, -1),"79",true));
    items.add(new ListItem(mMenuTitles[3], navMenuIcons.getResourceId(3, -1),"229",true));
    /**
     * listener of sliding menu
     */




private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {

    case 1:
        fragment = new SocietyFragment();
        break;
    case 2:

        break;
    case 3:

        break;
    case 4:

        break;
    case 5:

        break;

    default:
        break;
    }

}

logcat.txt:

03-16 14:58:05.300:E / AndroidRuntime(3796):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)

1 个答案:

答案 0 :(得分:0)

看起来listview inflate返回null。因为你从活动中膨胀了。相反,试试这个:

public void onViewCreated (View view, Bundle savedInstanceState) {
    listView = (ListView) view.findViewById(R.id.list_societe);
}