tom-james tom-james - 3 months ago 50
Java Question

Android Crashing when using a FragmentPagerAdapter

Okay, I hope someone may be able to help. I have spent tonight trying figure this out, I just seem to understand it.

I have created a translation app using 4 Fragments & FragmentPagerAdapter, however it runs fine on the surface up to when I click on the 4th column and then the app crashes and closes.

Logcat is cut down... the top section shows first sign why it crashes, section shows fatal exception and dies. When scrolling across it shows different names than it states on the tab E.g - see below for log cat

- Numbers show for Numbers,

- Family show for Colors,

- Colors show for Phrases,

- Phrases show nothing.

NumbersFragment{...} not updated inline; expected state 3 found 2
.../com.example.android.miwok W/FragmentManager:moveToState: Fragment state for ColorsFragment{ccf160c #2 id=0x7f0d006b android:switcher:2131558507:2} not updated inline; expected state 3 found 2
...W/FragmentManager: moveToState: Fragment state for PhrasesFragments{...} not updated inline; expected state 3 found 2
...D/MediaPlayer: setSubtitleAnchor in MediaPlayer
...D/AudioManager: AudioManager dispatching onAudioFocusChange(-2) for android.media.AudioManager@d5174d4com.example.android.miwok.ColorsFragment...
...D/AndroidRuntime: Shutting down VM
...E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.android.miwok, PID: 28858

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.pause()' on a null object reference
at ...ColorsFragment$1.onAudioFocusChange(ColorsFragment.java:26)
at ...AudioManager$FocusEventHandlerDelegate$1.handleMessage(AudioManager.java:2147) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Please see my java code below

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set content of activity to layout file
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CategoryFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}


x4 CategoryFragments.java are identical:

public class ColorsFragment extends Fragment {
private MediaPlayer mMediaPlayer;
private AudioManager mAudioManager; // <-- handles audiofocus when sound is played
AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
mMediaPlayer.pause(); // <-- Lose audio focus
mMediaPlayer.seekTo(0); // <-- & start again because its a short clip
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mMediaPlayer.start(); // <-- Resume playing media
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
releaseMediaPlayer(); // if audiofocus is lost or app is closed
// stop playback and clean resource.
}
}
};
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releaseMediaPlayer();
}
};
public ColorsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
// Create a list of words
final ArrayList<Word> words = new ArrayList<>();
words.add(new Word("red", "weṭeṭṭi", R.raw.color_red, R.drawable.color_red));
words.add(new Word("green", "chokokki", R.raw.color_green, R.drawable.color_green));
words.add(new Word("brown", "ṭakaakki", R.raw.color_brown, R.drawable.color_brown));
words.add(new Word("gray", "ṭopoppi", R.raw.color_gray, R.drawable.color_gray));
words.add(new Word("black", "kululli", R.raw.color_black, R.drawable.color_black));
words.add(new Word("white", "kelelli", R.raw.color_white, R.drawable.color_white));
words.add(new Word("dusty yellow", "ṭopiisә", R.raw.color_dusty_yellow, R.drawable.color_dusty_yellow));
words.add(new Word("mustard yellow", "chiwiiṭә", R.raw.color_mustard_yellow, R.drawable.color_mustard_yellow));
WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_colors);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
// its important to request audio focus straight away in the program.
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); // <-- turns variable to an instance
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Word word = words.get(position); // <-- Gets the Word() object, at the given position.
releaseMediaPlayer(); // <-- Releases anything that maybe in the media player.
// Request audio focus for playback
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
return rootView;
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public void onStop() {
super.onStop();
releaseMediaPlayer();
}
}


CategoryFragmentPagerAdapter

public class CategoryFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
private Context mContext;
public CategoryFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new NumbersFragment();
} else if (position == 1) {
return new FamilyFragments();
} else if (position == 2) {
return new ColorsFragment();
} else {
return new PhrasesFragments();
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
if (position == 0) {
return mContext.getResources().getString(R.string.category_numbers);
} else if (position == 1) {
return mContext.getResources().getString(R.string.category_family);
} else if (position == 2) {
return mContext.getResources().getString(R.string.category_colors);
} else {
return mContext.getResources().getString(R.string.category_phrases);
}
}
}[![enter image description here][1]][1]

Answer

According crash log you need to secure access to the mMediaPlayer as it could be null:

AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener =
        new AudioManager.OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                if (mMediaPlayer == null)
                    return;
                if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                        focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                    mMediaPlayer.pause();   // <-- Lose audio focus
                    mMediaPlayer.seekTo(0); // <--  & start again because its a short clip
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    mMediaPlayer.start();   // <-- Resume playing media
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                    releaseMediaPlayer();   // if audiofocus is lost or app is closed
                    //              stop playback and clean resource.
                }
            }
        };

I guess listener received AudioManager.AUDIOFOCUS_LOSS after which mMediaPlayer become null. And then it received next event crash happened