I'm developing a music player app for Android. I've only created an Activity with 4 fragments with empty layouts using ViewPager. It works great upto this point.
The fragments are Artists, Albums, All Songs and Playlists.
Now, I'm trying to fill the All Songs fragment with a list of all songs on the device. I've added the necessary code for that and the app compiles without any error. However, when I open the app, I get a force close with the message "Unfortunately, Player has stopped". So Here's the logcat.
E/AndroidRuntime( 2539): FATAL EXCEPTION: main
E/AndroidRuntime( 2539): java.lang.NullPointerException
E/AndroidRuntime( 2539): at com.example.player.AllSongsFragment.onCreate(AllSongsFragment.java:26)
E/AndroidRuntime( 2539): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
E/AndroidRuntime( 2539): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
E/AndroidRuntime( 2539): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:834)
E/AndroidRuntime( 2539): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:841)
E/AndroidRuntime( 2539): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
E/AndroidRuntime( 2539): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
E/AndroidRuntime( 2539): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
E/AndroidRuntime( 2539): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
E/AndroidRuntime( 2539): at android.view.View.measure(View.java:15775)
E/AndroidRuntime( 2539): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
E/AndroidRuntime( 2539): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 2539): at android.view.View.measure(View.java:15775)
E/AndroidRuntime( 2539): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
E/AndroidRuntime( 2539): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime( 2539): at android.view.View.measure(View.java:15775)
E/AndroidRuntime( 2539): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
So, the problem is a NullPointerException in the onCreate method of the AllSongsFragment class
Here's the content of AllSongsFragment.java
package com.example.player;
import com.example.player.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.net.Uri;
import android.content.ContentResolver;
import android.database.Cursor;
import android.widget.ListView;
import com.example.player.adapter.AllSongsAdapter;
import com.example.player.Song;
public class AllSongsFragment extends Fragment {
private ArrayList<Song> songArray;
private ListView songListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
songListView = (ListView)getView().findViewById(R.id.all_songs_list);
songArray = new ArrayList<Song>();
getSongList();
// Sort the songs
Collections.sort(songArray, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
AllSongsAdapter songAdt = new AllSongsAdapter(getActivity(), songArray);
songListView.setAdapter(songAdt);
} // onCreate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.all_songs_fragment_layout, container, false);
return rootView;
} //onCreateView()
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
// Iterate over the List
if(musicCursor!=null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songArray.add(new Song(thisId, thisTitle, thisArtist));
} while (musicCursor.moveToNext());
} // if
} //getSongList()
} //AllSongsFragment
I have a Song class in Song.java to represent each song object.
package com.flairsome.player;
public class Song {
private long id;
private String title;
private String artist;
//Constructor
public Song(long songID, String songTitle, String songArtist) {
id=songID;
title=songTitle;
artist=songArtist;
}
// get methods
public long getID() {
return id;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
} //.Song
The AllSongsAdapter.java is
package com.example.player.adapter;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.player.Song;
import com.example.player.R;
public class AllSongsAdapter extends BaseAdapter {
private ArrayList<Song> songs;
private LayoutInflater songInf;
// constructor
public AllSongsAdapter(Context c, ArrayList<Song> theSongs){
songs = theSongs;
songInf = LayoutInflater.from(c);
}
@Override
public int getCount() {
return songs.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//map to song layout
LinearLayout songLay = (LinearLayout)songInf.inflate(R.layout.song_item_layout, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
} //getView()
}
Although I've posted huge chunka of code, the problem, as seen from the logcat, seems to be in the onCreate() method in the AllSongsFragment class.
Is there anything that I am doing wrong?
Please leave a comment if you need more information or if I need to improve the question. Thank you.
Move this
songListView = (ListView)getView().findViewById(R.id.all_songs_list);
to onActivityCreated
getView()
returns null in your case. songListView
is null and you when you set adapter to listview results in NUllPointerException
You can move the below also to onActivityCreated
songArray = new ArrayList<Song>();
getSongList();
// Sort the songs
Collections.sort(songArray, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});
AllSongsAdapter songAdt = new AllSongsAdapter(getActivity(), songArray);
songListView.setAdapter(songAdt);
Replace songListView = (ListView)getView().findViewById(R.id.all_songs_list);
with songListView = (ListView)findViewById(R.id.all_songs_list);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.