在Fragment中使用listview时出现NullPointer异常

时间:2013-12-01 11:54:18

标签: android listview android-listview android-fragments nullpointerexception

我正在我的活动中实现自定义列表视图。 这是代码:

    public class ScoreCard extends ActionBarActivity {


        ArrayList<Song> songs;
        int score;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent intent = getIntent();
            songs = intent.getParcelableArrayListExtra("Songs");
            score = intent.getIntExtra("Score", 0);
            setContentView(R.layout.activity_score_card);

            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
            }
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.score_card, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_score_card, container, false);
                ListView listView = (ListView) findViewById(R.id.trackList);
                ArrayList<String> tracks = new ArrayList<String>();
                //Drawable [] art = new Drawable[tracks.size()];

                Log.d("Ayush", songs.size()+"");
                Log.d("Ayush", score+"");

                for(Song out : songs) {

                    tracks.add(out.gettracktitle());
                    Log.d("Ayush", out.gettracktitle());
                }

                ScoreCardList adapter = new ScoreCardList(getActivity(), tracks);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, final View view,
                                            int position, long id) {

                        view.animate().setDuration(2000).alpha(0)
                                .withEndAction(new Runnable() {
                                    @Override
                                    public void run() {

                                        view.setAlpha(1);
                                    }
                                });
                    }

                });

                return rootView;
            }


        }
    }

应用程序在listView.setAdapter(adapter)行崩溃,说它导致空指针异常。在调试模式下,我观察到,在这一行,对象,适配器不是null,而是listView。从断点继续,应用程序崩溃。这是我的适配器的代码:

public class ScoreCardList extends ArrayAdapter<String>{

    private final Activity context;
    private final ArrayList<String> web;
    //private final Drawable[] imageId;

    public ScoreCardList(Activity context,
                      ArrayList<String> web) {
        super(context, R.layout.score_card, web);
        this.context = context;
        this.web = web;
        //this.imageId = imageId;

    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.score_card, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.trackName);
        //ImageView imageView = (ImageView) rowView.findViewById(R.id.art);
        txtTitle.setText(web.get(position));
        //imageView.setImageDrawable(imageId[position]);
        return rowView;
    }
}

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:4)

The application crashes at the line listView.setAdapter(adapter), saying it caused a null pointer exception.

更改此

ListView listView = (ListView) findViewById(R.id.trackList);

ListView listView = (ListView) rootView.findViewById(R.id.trackList);