When you reopen the Activity, clicking in the notification variable stream is empty. How can this be fixed?

MainActivity

public class MainActivity extends AppCompatActivity { private ListView list; private final String RADIO = "ссылка на радио"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.list); String[] radios = getResources().getStringArray(R.array.kavkazradios); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, radios); list.setAdapter(adapter); } @Override protected void onResume() { super.onResume(); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { CharSequence strCharSequence = ((TextView) view).getText(); String str = strCharSequence.toString().trim(); Intent intent = new Intent(MainActivity.this, Player.class); if (str.equalsIgnoreCase("Радио")) { intent.putExtra("stream", RADIO); intent.putExtra("radio", str); intent.putExtra("rec", 1); startActivity(intent); } } }); } @Override protected void onDestroy() { NotificationManager notifManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifManager.cancelAll(); super.onDestroy(); } } 

PlayerActivity

 public class Player extends AppCompatActivity { private String stream; private String radio; private int rec; private boolean isPlay; private String MAYBE_ACTION = "MAYBE_ACTION"; private ImageButton btnPLayPause; private TextView txtRadio; private TextView txtMusic; NotificationManager notificationManager; BandwidthMeter bandwidthMeter; TrackSelector trackSelector; SimpleExoPlayer player; private SimpleExoPlayerView simpleExoPlayerView; public static String LOG_TAG = "my_log"; Timer timer; TimerTask timerTask; BroadcastReceiver br; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause); txtRadio = (TextView) findViewById(R.id.txtRadio); txtMusic = (TextView) findViewById(R.id.txtMusic); stream = getIntent().getExtras().getString("stream").toString().trim(); radio = getIntent().getExtras().getString("radio").toString().trim(); rec = getIntent().getExtras().getInt("rec"); setTitle(radio); 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. if (stream.contains("m3u8")) { MediaSource mediaSource = new HlsMediaSource(streamUri, dataSourceFactory, 1, null, null); // Prepare the player with the source. player.prepare(mediaSource); } else { 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); 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) .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); br = new BroadcastReceiver() { @Override // действия при получении сообщений public void onReceive(Context context, Intent intent) { finish(); } }; IntentFilter intentFilter = new IntentFilter(MAYBE_ACTION); registerReceiver(br, intentFilter); txtRadio.setText("Сейчас играет: " + radio); } private BroadcastReceiver NetworkChangeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String status = NetworkUtil.getConnectivityStatusString(context); Toast.makeText(context, status, Toast.LENGTH_LONG).show(); } }; @Override protected void onResume() { super.onResume(); registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED")); } @Override protected void onDestroy() { NotificationManager notifManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifManager.cancelAll(); super.onDestroy(); player.release(); if (NetworkChangeReceiver != null) unregisterReceiver(NetworkChangeReceiver); 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); } } } } 

That's what's in the market

 java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.sergey89.radios/ru.sergey89.radios.Player}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243) at android.app.ActivityThread.access$1000(ActivityThread.java:218) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6917) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at ru.sergey89.radios.Player.onCreate(Player.java:118) at android.app.Activity.performCreate(Activity.java:6609) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086) ... 10 more 
  • Please show the code with which you are creating PendingIntent. - tse
  • I added both Activity codes to the question. Full versions - Sergey

2 answers 2

You do not add any extra to the intent:

  Intent intent = new Intent(this, Player.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

Therefore, this getExtras () in onCreate () naturally returns null with all the consequences:

 stream = getIntent().getExtras().getString("stream").toString().trim(); 
  • it’s written in my code like that - Sergey
  • Yes, this is your code. The first line is an intent that will trigger the activation. The second is the packaging of this intent inside the pending intent for transmission with notification. The problem is that the intent created in the first line does not contain any extra. And the activity awaits them and does not even check their presence, so it falls. - tse
  • Aa realized that you need to write something like this after the first line intent.putExtra ("stream", stream); intent.putExtra ("radio", radio); intent.putExtra ("rec", rec); - Sergey
  • And why did this not always happen? sometimes you poke a notification into Player starts up normally, and sometimes crash - Sergey
  • Probably because it does not crash when this activation is already running (by that other intent, which is with extra), and by the notification the application only becomes visible. - tse

You must put the variable stream in the intent , since This activity will be opened by this intent.

 Intent intent = new Intent(this, Player.class); intent.putExtra("stream", stream); 
  • I have it. But sometimes when I open the Player activity from the notification, the application crashes. The application is already laid out in the market and there showed 2 failures, there is even an exception. I'll put my question in my question - Sergey