Reputation: 105
I have been blocked for 2 days, I have an exception on a radio button for a change of language of the application, activity stop with nullPointerException on a null objet reference,
"NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference"
strings file xml are ok, do you see something that I do not see? here is the code :
public class ArtistActivity extends AppCompatActivity implements View.OnClickListener {
TextView name;
ImageView img;
TextView music;
String title;
String musicArtist;
MaterialButton btnPlay;
MaterialButton btnStop;
TextView titleMusicArtist;
MediaPlayer mediaPlayer = new MediaPlayer();
RadioGroup rdioGrp;
// information artist
String imgUrl = "https://st3.depositphotos.com/1008939/12603/i/950/depositphotos_126032722-stock-photo-roaring-singing-woman.jpg";
String musicUrl = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-4.mp3";
String siteUrl = "https://sites.google.com/view/bestrecord/accueil?authuser=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artist);
// button music, playlist and new playlist
LinearLayout linearPlaylist = findViewById(R.id.linearPlaylist);
LinearLayout linearNewPlaylist = findViewById(R.id.linearNewPlaylist);
LinearLayout linearMusic = findViewById(R.id.linearMusic);
linearPlaylist.setOnClickListener(this);
linearNewPlaylist.setOnClickListener(this);
linearMusic.setOnClickListener(this);
linearMusic.setTag(0);
linearPlaylist.setTag(1);
linearNewPlaylist.setTag(2);
// button play
btnPlay = findViewById(R.id.btn_play_music_artist);
btnPlay.setOnClickListener(this);
btnPlay.setTag(3);
// button stop
btnStop = findViewById(R.id.btn_stop_music_artist);
btnStop.setOnClickListener(this);
btnStop.setTag(4);
// textView title Music
titleMusicArtist = findViewById(R.id.txtLastMusicPlayed);
// display artist information
name = findViewById(R.id.txtViewTitleArtist);
img = findViewById(R.id.imageArtist);
music = findViewById(R.id.txtViewMusicArtist);
// image artiste
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
InputStream input = new java.net.URL(imgUrl).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
// name and song title artist
Scrape scrapeTitle = new Scrape();
scrapeTitle.execute();
// prepare music artist
if (!mediaPlayer.isPlaying()) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(musicUrl);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
// language configuration
init();
}
// scrape information artist
@SuppressLint("StaticFieldLeak")
private class Scrape extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
Document document = null;
try {
// Connect to the website
document = Jsoup.connect(siteUrl).get();
} catch (IOException e) {
e.printStackTrace();
}
// take name artist
assert document != null;
List<String> e = document.getElementsByClass(" jgG6ef").eachText();
title = e.get(0)+" "+e.get(1);
name.setText(title);
// take song title artist
List<String> ee = document.getElementsByAttributeValue("id","h.s0qhywz0cts2").eachText();
musicArtist = ee.get(0);
music.setText(musicArtist);
return null;
}
@Override
protected void onPostExecute(Void aVoid) { }
}
// create menu application
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_mainactivity, menu);
return true;
}
// menu buttons music / playlist / new playlist
public void onClick(View v) {
int btnInt = ( int ) v.getTag();
// button music clicked
if ( btnInt == 0 ) {
// go to page music
Intent musicIntent = new Intent(ArtistActivity.this, MainActivity.class );
startActivity( musicIntent );
// button playlist clicked
} else if ( btnInt == 1 ) {
// go to page playlist
Intent PlaylistIntent = new Intent(ArtistActivity.this, PlaylistActivity.class );
startActivity( PlaylistIntent );
// button new playlist clicked
}else if ( btnInt == 2 ) {
// go to page new playlist
Intent newPlaylistIntent = new Intent(ArtistActivity.this, NewPlaylistActivity.class );
startActivity( newPlaylistIntent );
}
// end menu button
// button play/pause
else if( btnInt == 3) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
btnPlay.setIconResource(R.drawable.ic_baseline_play_arrow_24);
} else if(!mediaPlayer.isPlaying()) {
mediaPlayer.start();
if(mediaPlayer.isPlaying()) btnPlay.setIconResource(R.drawable.ic_pause);
}
}
// button stop
else if( btnInt == 4) {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(musicUrl);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// menu hamburger, items clicked
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.artiste:
Toast.makeText(ArtistActivity.this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.site_officiel:
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://sites.google.com/view/bestrecord/accueil?authuser=1"));
startActivity(intent);
break;
case R.id.contact:
Intent contactIntent = new Intent(ArtistActivity.this, ContactBestRecord.class );
startActivity( contactIntent );
break;
case R.id.langue:
AlertDialog.Builder alertLang = new AlertDialog.Builder( this );
alertLang.setView( R.layout.layout_test )
.create()
.show();
break;
case R.id.about:
AlertDialog.Builder alert = new AlertDialog.Builder( this );
alert.setView( R.layout.about_us )
.create()
.show();
break;
}
return super.onOptionsItemSelected(item);
}
// end menu
// change language
public void init() {
rdioGrp = findViewById(R.id.rdioGrpLang);
rdioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@SuppressLint("NonConstantResourceId")
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if (rb!=null){
switch(checkedId) {
case R.id.radioBtnFr:
changeLang("fr");
break;
case R.id.radioBtnEn:
changeLang("en");
break;
case R.id.radioBtnIt:
changeLang("it-rIT");
break;
default:
changeLang("fr");
}
}
}
});
}
// method to change language
public void changeLang(String l) {
Locale locale = new Locale(l);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
recreate();
}
}
the layout call in dialBox layout "test" :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border_layout_about_us"
android:padding="@dimen/dim_15">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/chooseLanguage"
android:textColor="@color/white"
android:textSize="@dimen/dim_20"
android:gravity="center"
tools:ignore="SpUsage"
android:id="@+id/txtLangue"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLangue"
android:orientation="horizontal"
android:gravity="center"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="@dimen/dim_10"
android:id="@+id/rdioGrpLang"
tools:ignore="UselessParent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnFr"
android:checked="true"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:text="@string/langueFr">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnEn"
android:text="@string/langueEn"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:layout_marginLeft="@dimen/dim_10"
android:layout_marginRight="@dimen/dim_10">
</RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtnIt"
android:buttonTint="@color/white"
android:textColor="@color/white"
android:text="@string/langueIt">
</RadioButton>
</RadioGroup>
</LinearLayout>
Upvotes: 0
Views: 605
Reputation: 552
Delete -//language configuration- In Your Code And
Edit The onOptionsItemSelected method as follows:
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
....
....
case R.id.langue:
AlertDialog alert = new AlertDialog.Builder( this ).setView( R.layout.choose_language).create();
alert.show();
rdioGrp = alert.findViewById(R.id.rdioGrpLang);
setRadioListener();
break;
....
....
} // End onOptionsItemSelected Mehtod
private void setRadioListener() {
rdioGrp.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@SuppressLint("NonConstantResourceId")
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton)
group.findViewById(checkedId);
if (rb!=null){
switch(checkedId) {
case R.id.radioBtnFr:
changeLang("fr");
break;
case R.id.radioBtnEn:
changeLang("en");
break;
case R.id.radioBtnIt:
changeLang("it-rIT");
break;
default:
app.l("iiii");
}
}
}
});
}
public void changeLang(String l) {
Locale locale = new Locale(l);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
recreate();
}
Upvotes: 1