Reputation: 1750
I am implementing a custom listview in my activity. Here is the code :
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;
}
}
}
The application crashes at the line listView.setAdapter(adapter), saying it caused a null pointer exception. In debug mode I observed that, at this line the object, adapter, is not null, but the listView is. Upon continuation from the breakpoint, the application crashed. Here is the code for my adapter :
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;
}
}
Please help. Thanks !
Upvotes: 0
Views: 1471
Reputation: 133570
The application crashes at the line listView.setAdapter(adapter), saying it caused a null pointer exception.
Change this
ListView listView = (ListView) findViewById(R.id.trackList);
to
ListView listView = (ListView) rootView.findViewById(R.id.trackList);
Upvotes: 4