0

Я реализую пользовательский listview в своей деятельности. Вот код:Исключение NullPointer при использовании listview в Fragment

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 (адаптер), мотивировав это вызвало исключения нулевого указателя. В режиме отладки я заметил, что на этой строке объект, адаптер, не является нулевым, но 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; 
    } 
} 

Пожалуйста, помогите. Благодаря !

ответ

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); 
Смежные вопросы