如何将自定义listView添加到片段?

时间:2014-11-25 07:07:10

标签: android android-fragments nullpointerexception android-listfragment

我想在我的片段中尝试自定义列表视图但是得到一个空指针错误,我该如何解决这个问题..我是初学者。

listmain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="5dp"
       android:listSelector="@drawable/list_row_selector" />

</RelativeLayout>

activity_main.xml中:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
  </FrameLayout>

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

我的主要活动是导航抽屉:

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#009933")));
        //getActionBar().setIcon(
               // new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment(this);
            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

这是我的片段:

public class HomeFragment extends ListFragment {

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

    @SuppressLint("NewApi")
    public HomeFragment(Activity acticity){

        this.activity=activity;
    }


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

        rootView = inflater.inflate(R.layout.listmain, container, false);
        listView = (ListView) rootView.findViewById(R.id.list);
        adapter = new CustomListAdapter(getActivity(), movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // changing action bar color


        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {

                Log.d(TAG, response.toString());
                hidePDialog();

                // Parsing json
                for (int i = 0; i < response.length(); i++) {

                    try {

                        JSONObject obj = response.getJSONObject(i);
                        Movie movie = new Movie();
                        movie.setTitle(obj.getString("title"));
                        movie.setThumbnailUrl(obj.getString("image"));
                        movie.setRating(((Number) obj.get("rating")).doubleValue());
                        movie.setYear(obj.getInt("releaseYear"));

                        // Genre is json array
                        JSONArray genreArry = obj.getJSONArray("genre");
                        ArrayList<String> genre = new ArrayList<String>();
                        for (int j = 0; j < genreArry.length(); j++) {

                            genre.add((String) genreArry.get(j));
                        }
                        movie.setGenre(genre);

                        // adding movie to movies array
                        movieList.add(movie);

                    } catch (JSONException e) {

                        e.printStackTrace();
                    }
                }

                // notifying list adapter about data changes
                // so that it renders the list view with updated data
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {}

    @Override
    public void onDestroy() {

        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {

        if (pDialog != null) {

            pDialog.dismiss();
            pDialog = null;
        }
    }
}

这是我的惯例:

public class CustomListAdapter extends BaseAdapter {

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

    public CustomListAdapter( Activity activity, List<Movie> movieItems) {

        this.activity = activity;
        this.movieItems = movieItems;
        //this.context = context;
    }

    @Override
    public int getCount() {

        return movieItems.size();
    }

    @Override
    public Object getItem(int location) {

        return movieItems.get(location);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (inflater == null)
            inflater = (LayoutInflater) activity.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;
    }
}

我的日志:


11-25 06:41:20.813: W/dalvikvm(1135): threadid=1: thread exiting with uncaught exception (group=0xa4c18648)
11-25 06:41:20.821: E/AndroidRuntime(1135): FATAL EXCEPTION: main
11-25 06:41:20.821: E/AndroidRuntime(1135): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.NullPointerException
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.os.Looper.loop(Looper.java:137)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at java.lang.reflect.Method.invoke(Method.java:525)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at dalvik.system.NativeStart.main(Native Method)
11-25 06:41:20.821: E/AndroidRuntime(1135): Caused by: java.lang.NullPointerException
11-25 06:41:20.821: E/AndroidRuntime(1135):     at info.androidhive.slidingmenu.listadapter.CustomListAdapter.<init>(CustomListAdapter.java:28)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at info.androidhive.slidingmenu.HomeFragment.onCreateView(HomeFragment.java:57)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.Fragment.performCreateView(Fragment.java:1695)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.Activity.performStart(Activity.java:5142)
11-25 06:41:20.821: E/AndroidRuntime(1135):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-25 06:41:20.821: E/AndroidRuntime(1135):     ... 11 more

2 个答案:

答案 0 :(得分:0)

在JSON处理发生之前,您正在调用customListAdapter。因此,传递给customListAdapter的movielist没有任何值。这就是为什么你得到一个空指针异常。

解决方案:在JSON处理完成后执行以下代码行

listView.setAdapter(适配器);

您正在尝试的最佳方法是使用AsyncTask从Server获取数据,并在AsyncTask的onPostExecute方法中执行该行

listView.setAdapter(适配器);

答案 1 :(得分:0)

最初,您的movieList为空,没有任何数据。

adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);

您只需要使用onResponse()方法而非onCreateView()方法移动上述代码。所以看起来像

        @Override
        public void onResponse(JSONArray response) {.....
             adapter = new CustomListAdapter(getActivity(), movieList);
             listView.setAdapter(adapter);
             adapter.notifyDataSetChanged();
        }.......

在此方法中,您的数据和用户界面将会更新。