There is an activity in which from the first we get a link to the audio stream and play it through ExoPlayer. Also in this activates a notification is built, where information about the current stream and the "Stop" button are displayed. Clicking on the notification opens this activation.

Sometimes (not in all cases) when you click on a notification, the application crashes. In the market, there were about 2000 failures and all come with one mistake practically. As I understand it, the stream variable is empty when I click on the notification. How to fix this moment, maybe I made a mistake in the code? Here is the player code itself

public class Player extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private String stream; private String radio; private int img; private boolean isPlay; private String MAYBE_ACTION = "MAYBE_ACTION"; private ImageButton btnPLayPause; private TextView txtRadio; private CircleImageView imgRadio; NotificationManager notificationManager; BandwidthMeter bandwidthMeter; TrackSelector trackSelector; SimpleExoPlayer player; private SimpleExoPlayerView simpleExoPlayerView; BroadcastReceiver br; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause); txtRadio = (TextView) findViewById(R.id.txtRadio); imgRadio = (CircleImageView) findViewById(R.id.imgRadio); //ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ тСкст ΠΈΠ· списка ΠΈ ссылку Π½Π° Ρ€Π°Π΄ΠΈΠΎ stream = getIntent().getExtras().getString("stream").toString().trim(); radio = getIntent().getExtras().getString("radio").toString().trim(); img = getIntent().getExtras().getInt("img"); imgRadio.setImageResource(img); bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory streamSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter); trackSelector = new DefaultTrackSelector(streamSelectionFactory); player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, new DefaultLoadControl()); simpleExoPlayerView = new SimpleExoPlayerView(this); simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view); //Set media controller simpleExoPlayerView.setUseController(true); simpleExoPlayerView.requestFocus(); // Bind the player to the view. simpleExoPlayerView.setPlayer(player); Uri streamUri = Uri.parse(stream); // Measures bandwidth during playback. Can be null if not required. DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); // Produces DataSource instances through which media data is loaded. DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"), bandwidthMeter); // Produces Extractor instances for parsing the media data. ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); // This is the MediaSource representing the media to be played. MediaSource mediaSource = new ExtractorMediaSource(streamUri, dataSourceFactory, extractorsFactory, null, null); player.prepare(mediaSource); player.setPlayWhenReady(true); isPlay = true; if (player.getPlayWhenReady() == true) { btnPLayPause.setImageResource(R.drawable.icon_stop); } /*Π‘Π»ΠΎΠΊ для создания ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠΉ*/ notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // НамСрСниС для запуска Π²Ρ‚ΠΎΡ€ΠΎΠΉ активности Intent intent = new Intent(this, Player.class); intent.putExtra("stream", stream); intent.putExtra("radio", radio); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); //НамСрСниС для закрытия активности, Ссли Π½Π°ΠΆΠ°Π»ΠΈ ΠΊΠ½ΠΎΠΏΠΊΡƒ Π‘Ρ‚ΠΎΠΏ Intent closeIntent = new Intent(); closeIntent.setAction(MAYBE_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT); // Π‘Ρ‚Ρ€ΠΎΠΈΠΌ ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ Notification builder = new Notification.Builder(this) .setTicker(radio) .setContentTitle("Π˜Π³Ρ€Π°Π΅Ρ‚: " + radio) /*.setContentText("Π­Ρ‚ΠΎ я, ΠΏΠΎΡ‡Ρ‚Π°Π»ΡŒΠΎΠ½ ΠŸΠ΅Ρ‡ΠΊΠΈΠ½. ΠŸΡ€ΠΈΠ½Π΅Ρ для вас посылку")*/ .setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pIntent) .addAction(R.drawable.icon_stop, "Π‘Ρ‚ΠΎΠΏ", pendingIntent) .build(); //НастойчивоС ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ builder.flags = builder.flags | Notification.FLAG_INSISTENT | Notification.FLAG_ONGOING_EVENT; notificationManager.notify(0, builder); /*Π‘Π»ΠΎΠΊ для создания ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠΉ*/ /*BroadcastReceiver*/ br = new BroadcastReceiver() { @Override // дСйствия ΠΏΡ€ΠΈ ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠΈ сообщСний public void onReceive(Context context, Intent intent) { finish(); } }; // создаСм Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ для BroadcastReceiver IntentFilter intentFilter = new IntentFilter(MAYBE_ACTION); // рСгистрируСм (Π²ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ) BroadcastReceiver registerReceiver(br, intentFilter); /*BroadcastReceiver*/ txtRadio.setText(radio); } @Override protected void onDestroy() { NotificationManager notifManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifManager.cancelAll(); super.onDestroy(); player.release(); unregisterReceiver(br); } public void onPlayPause(View view) { if (isPlay == true) { if (player.getPlayWhenReady() == true) { player.setPlayWhenReady(false); isPlay = false; btnPLayPause.setImageResource(R.drawable.icon_play); } } else if (isPlay == false) { if (player.getPlayWhenReady() == false) { player.setPlayWhenReady(true); isPlay = true; btnPLayPause.setImageResource(R.drawable.icon_stop); } } } } 

This is a mistake

 java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2439) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2499) at android.app.ActivityThread.access$900 (ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1360) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:148) at android.app.ActivityThread.main (ActivityThread.java:5487) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:765) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:655) Caused by: java.lang.NullPointerException: at ru.ars.radio.Player.onCreate (Player.java:134) at android.app.Activity.performCreate (Activity.java:6556) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2392) 

Drain 134 is

 stream = getIntent().getExtras().getString("stream").toString().trim(); 

Please tell me how to rewrite this piece of code more correctly? And why such an exception is thrown

  • Read about flags in PendingIntent . Crash, because when you start onCreate empty extras due to incorrect flag 0 when setting PendingIntent . - eugeneek
  • Thank you, read the info on your link - Scorpion

0