我正在为Android开发音乐播放器应用。我只使用ViewPager创建了一个包含4个空布局片段的Activity。到目前为止它很有效。
片段是艺术家,专辑,所有歌曲和播放列表。
现在,我尝试用设备上所有歌曲的列表填充All Songs片段。我已为此添加了必要的代码,应用程序编译时没有任何错误。但是,当我打开应用程序时,我会收到一条关闭消息的力量"不幸的是,玩家已停止"。所以这里是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)
所以,问题是AllSongsFragment类的onCreate方法中的NullPointerException
这里是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
我在Song.java中有一个Song类来表示每个歌曲对象。
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
AllSongsAdapter.java是
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()
}
虽然我发布了大量的代码,但是从logcat看到的问题似乎出现在AllSongsFragment类的onCreate()方法中。
我有什么问题吗?
如果您需要更多信息或我需要改进问题,请发表评论。谢谢。
答案 0 :(得分:3)
移动此
songListView = (ListView)getView().findViewById(R.id.all_songs_list);
到onActivityCreated
getView()
在您的情况下返回null。当您在songListView
NUllPointerException
为空
您也可以将以下内容移至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);
答案 1 :(得分:0)
将songListView = (ListView)getView().findViewById(R.id.all_songs_list);
替换为songListView = (ListView)findViewById(R.id.all_songs_list);