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
onCreateemptyextrasdue to incorrect flag0when settingPendingIntent. - eugeneek