I am developing audio player. I can play any music on audio player when I play music than it create notification for music.
Audio player working prefect for playing music, but my problem is that when I press on notification icon it show me null pointer exception to jumping playerActivity, I know uri is null due to destroying Activity but I can't understand how to handle this problem.
Here is my code
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);
}
}
when press on item it get uri and send into intent to PlayerAudio Activity PlayerAudio Activity getdata()
is function that is used to get data from intent
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);
}
}
and inside on start() function service start and play music and also show notification when i press notification it show null pointer exception on this function
File file = new File(sourceUri.toString();
if (file != null && file.length() != 0) {
String filename = file.getName();
txt_name.setText(filename);
}
here is my notifactin code
@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);
}
here is my 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)
I know Source uri is null, how to handle this problem?
here is my solution i reached on problem with all of reply i read carefully comments than think where is mistake i found solution with help... All friends comments thanks a lot everyone
@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);
}
You are sending an object via intent
and trying to capture String
on reception.
You can implement the Serializable
interface
in the class
Audio
and then pass object instances in the intent extra using the putExtra(Serializable)
Example:
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"));