我正在开发音频播放器。我在播放音乐时可以在音频播放器上播放任何音乐而不是创建音乐通知。
音频播放器工作完美播放音乐,但我的问题是,当我按下通知图标时,它显示跳转playerActivity的空指针异常,我知道uri因为销毁Activity而无效,但我无法理解如何处理这个问题。
这是我的代码
adapter.setOnItemClickListner(new AudioDeatailAdapter.OnItemClckListner() {
@Override
public void OnClick(ArrayList<Audio> audioArrayList,int possition) {
String media = audioArrayList.get(possition).getData();
Intent intent = new Intent(AudioActivity.this,PlayerAudioActivity.class);
intent.putExtra("uri",media);
startActivity(intent);
}
}
按下项目时获取uri并将意图发送到PlayerAudio活动PlayerAudio活动getdata()
是用于从意图中获取数据的函数
public void getData() {
Intent intent = getIntent();
try {
sourceUri = Uri.parse(intent.getStringExtra("uri"));
}
catch (Exception e) {
e.printStackTrace();
sourceUri = intent.getData();
}
File file = new File(sourceUri.toString());
if (file != null && file.length() != 0) {
String filename = file.getName();
txt_name.setText(filename);
txt_name.setSelected(true);
}
}
并在start()函数内部启动并播放音乐,并在按下通知时显示通知,此函数显示空指针异常
File file = new File(sourceUri.toString();
if (file != null && file.length() != 0) {
String filename = file.getName();
txt_name.setText(filename);
}
这是我的notifactin代码
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
prerationListner.prepared();
this.handler.postDelayed(seekBarRunable, 100);
mp.start();
//notification
Intent notIntent = new Intent(this, PlayerAudioActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(pendInt)
.setSmallIcon(R.drawable.ic_play)
.setTicker(songTitle)
.setOngoing(true)
.setAutoCancel(true)
.setContentTitle("Playing")
.setContentText(songTitle);
Notification not = builder.build();
startForeground(NOTIFY_ID, not);
}
这是我的logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{studio.freeapp.com.jplayer/studio.freeapp.com.jplayer.activity.PlayerAudioActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at studio.freeapp.com.jplayer.activity.PlayerAudioActivity.getData(PlayerAudioActivity.java:319)
at studio.freeapp.com.jplayer.activity.PlayerAudioActivity.onCreate(PlayerAudioActivity.java:113)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424)
我知道Source uri为null,如何处理这个问题?
这是我的解决方案我遇到问题所有回复我仔细阅读评论而不是认为哪里是错误我找到解决方案的帮助...所有的朋友评论感谢很多人
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
prerationListner.prepared();
this.handler.postDelayed(seekBarRunable, 100);
mp.start();
//notification
Intent notIntent = new Intent(this, PlayerAudioActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notIntent.putExtra("uri", sourceUri);
notIntent.putExtra("playerpos", true);
PendingIntent pendInt = PendingIntent.getActivity(this, 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(pendInt)
.setSmallIcon(R.drawable.ic_play)
.setTicker(songTitle)
.setOngoing(true)
.setAutoCancel(true)
.setContentTitle("Playing")
.setContentText(songTitle);
Notification not = builder.build();
startForeground(NOTIFY_ID, not);
}
答案 0 :(得分:0)
您正在通过intent
发送一个对象,并尝试在接收时捕获String
。
您可以在Serializable
interface
中实施class
Audio
,然后使用putExtra(Serializable)
示例:
Audio media = audioArrayList.get(possition);
Intent intent = new Intent(AudioActivity.this,PlayerAudioActivity.class);
intent.putExtra("uri",media);
//On reception
sourceUri = Uri.parse(intent.getSerializableExtra("uri"));