Android使用FragmentPagerAdapter

时间:2016-09-02 22:51:39

标签: java android debugging

好的,我希望有人可以提供帮助。我今晚花了很多时间试图解决这个问题,我似乎只是理解它。

我使用4片段创建了一个翻译应用程序。 FragmentPagerAdapter,但是当我点击第4列然后应用程序崩溃并关闭时,它在表面上运行良好。

Logcat被削减......顶部显示第一个标志为什么它崩溃,部分显示致命异常并且死亡。滚动时显示的名称与选项卡E.g上显示的名称不同 - 见下面的日志猫
- 数字显示数字,
- 色彩家庭表演,
- 色彩表示短语,
- 短语没有显示任何内容。

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)

请参阅下面的我的java代码

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是相同的:

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]

1 个答案:

答案 0 :(得分:1)

根据崩溃日志,您需要保护mMediaPlayer的访问权限,因为它可能是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.
                }
            }
        };

我猜聆听者收到AudioManager.AUDIOFOCUS_LOSSmMediaPlayer变为空。然后它收到了下一次事件发生的事故