我的歌手:
public class songAdapter extends RecyclerView.Adapter<songAdapter.ViewHolder> {
ArrayList<songInfo> songs;
Context context;
OnitemClickListener onitemClickListener;
public songAdapter(ArrayList<songInfo> songs, Context context) {
this.songs = songs;
this.context = context;
}
public interface OnitemClickListener{
void onItemclick(ViewHolder holder, View v, songInfo obj, int position);
}
public void setOnitemClickListener(OnitemClickListener onitemClickListener)
{
this.onitemClickListener=onitemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView= LayoutInflater.from(context).inflate(R.layout.raw_songs,parent,false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final songInfo song=songs.get(position);
holder.songName.setText(song.songName);
holder.artistName.setText(song.artistName);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onitemClickListener!=null)
{
onitemClickListener.onItemclick(holder,view,song,position);
}
}
});
}
@Override
public int getItemCount() {
return songs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView songName,artistName;
public ViewHolder(View itemView) {
super(itemView);
songName=(TextView)itemView.findViewById(R.id.songname);
artistName=(TextView)itemView.findViewById(R.id.artistName);
}
}
}
this is my mainactivity
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private ArrayList<songInfo> songs=new ArrayList<songInfo>();
songAdapter songAdapter;
MediaPlayer mediaPlayer;
SeekBar seekBar;
Cursor cursor;
private Handler myHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
songAdapter=new songAdapter(songs, this);
recyclerView.setAdapter(songAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
linearLayoutManager.getOrientation());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
songAdapter.setOnitemClickListener(new songAdapter.OnitemClickListener() {
@Override
public void onItemclick(songAdapter.ViewHolder holder, View v, songInfo obj, int position) {
mediaPlayer.start();
System.gc();
int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
cursor.moveToPosition(position);
final String filename = cursor.getString(songPath);
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
mediaPlayer.start();
}else {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filename);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
seekBar.setProgress(0);
seekBar.setMax(mediaPlayer.getDuration());
Log.d("Prog", "run: " + mediaPlayer.getDuration());
}
});
// b.setText("Stop");
}catch (Exception e){}
}
};
myHandler.postDelayed(runnable,100);
}
}
});
checkUserPermission();
Thread t = new runThread();
t.start();
}
public class runThread extends Thread {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("Runwa", "run: " + 1);
if (mediaPlayer != null) {
seekBar.post(new Runnable() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
});
Log.d("Runwa", "run: " + mediaPlayer.getCurrentPosition());
}
}
}
}
private void checkUserPermission(){
if(Build.VERSION.SDK_INT>=23){
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},123);
return;
}
}
loadSongs();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 123:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
loadSongs();
}else{
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
checkUserPermission();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void loadSongs(){
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
cursor = getContentResolver().query(uri,null,selection,null,null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
songInfo s = new songInfo(name,artist,songPath);
songs.add(s);
}while (cursor.moveToNext());
}
// cursor.close();
songAdapter = new songAdapter(songs, MainActivity.this);
}
}
public void onDestroy() {
super.onDestroy();
cursor.close();
}
}
我从SD卡中获取歌曲并可以在屏幕上看到它们,但是当我尝试播放这些歌曲时,我的应用程序崩溃并出现以下错误。尝试调用虚拟方法'void android.media.MediaPlayer.start()'on空对象引用。
我不知道该怎么办请帮忙。
这是我的logcat
过程:com.example.murarilal.musicmania,PID:26119 java.lang.NullPointerException:尝试调用虚方法'void com.example.murarilal.musicmania.MainActivity $ 1.onItemclick(MainActivity.java:54)上的空对象引用上的android.media.MediaPlayer.start()' 在 com.example.murarilal.musicmania.songAdapter $ 1.onClick(songAdapter.java:54)**
答案 0 :(得分:1)
问题是您正在尝试访问MediaPlayer
的空实例,
尝试调用虚方法'void 关于空对象引用的android.media.MediaPlayer.start()'。
所以在方法mediaPlayer.start();
中执行onClick()
之前,您需要初始化MediaPlayer
实例:
MediaPlayer mediaPlayer = new MediaPlayer();
例如:
private MediaPlayer mediaPlayer; //* Create variable type MediaPlayer .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
mediaPlayer = new MediaPlayer(); //* Initialize MediaPlayer!
...
...
songAdapter.setOnitemClickListener(new songAdapter.OnitemClickListener() {
@Override
public void onItemclick(songAdapter.ViewHolder holder, View v, songInfo obj, int position) {
mediaPlayer.start(); //*Now you can use the MediaPlayer instance.
...
...