- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我的应用程序中,我有一个 ViewPager,在该 ViewPager 中大约有 17 个 fragment 。我设计了纵向和横向的布局。当我旋转我的屏幕时,方法 getView()
返回一个空对象,不知道为什么会这样,我使用了 setRetainInstance(true)
,但仍然同样,我使用 PagerStateFragmentAdapter
作为我的适配器实现。
为什么会这样?
提前谢谢你。
编辑
主 Activity
public class MainActivity extends FragmentActivity implements ViewPagerController{
public static final String READ_TYPE = "Read_type";
private static final String TAG = MainActivity.class.getSimpleName();
private int mReadType;
private ViewPager mBookPager;
@Override
public void goToNextPage() {
// TODO Auto-generated method stub
int currentPage = mBookPager.getCurrentItem();
if(currentPage+1 < mBookPager.getAdapter().getCount()){
FragmentPagerAdapter adapter = (FragmentPagerAdapter)mBookPager.getAdapter();
BookPageFragment bookPage = (BookPageFragment)adapter.getItem(currentPage);
bookPage.setCurrentPage(false);
bookPage = (BookPageFragment)adapter.getItem(currentPage+1);
bookPage.setCurrentPage(true);
mBookPager.setCurrentItem(currentPage+1, true);
}
}
@Override
public void goToPreviousPage() {
// TODO Auto-generated method stub
int currentPage = mBookPager.getCurrentItem();
if(currentPage >= 0){
mBookPager.setCurrentItem(currentPage-1,true);
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
overridePendingTransition(R.anim.anim_move_to_right,R.anim.anim_from_left_to_right);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG,"onDestroy");
mBookPager.removeAllViews();
mBookPager = null;
super.onDestroy();
}
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.main_layout);
overridePendingTransition(R.anim.anim_from_right_to_left, R.anim.anim_move_to_left);
initReadType();
mBookPager = (ViewPager)findViewById(R.id.mainLayout_mainViewPager);
initViewPager();
}
private void initReadType(){
mReadType = getIntent().getIntExtra(READ_TYPE, -1);
}
private void initViewPager(){
BookPagerAdapter mPagerAdapter = new BookPagerAdapter(getSupportFragmentManager(),mReadType);
((BookPageFragment)mPagerAdapter.getItem(0)).setCurrentPage(true);
mBookPager.setAdapter(mPagerAdapter);
mBookPager.setOnPageChangeListener(mOnPageChangeListener);
}
private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int selection) {
// TODO Auto-generated method stub
BookPagerAdapter bookAdapter = (BookPagerAdapter)mBookPager.getAdapter();
BookPageFragment fragment = (BookPageFragment)bookAdapter.getItem(selection);
if(selection > 0){
bookAdapter.getItem(selection - 1).onPause();
// bookAdapter.getItem(selection - 1).onDestroy();
}
if(selection + 1 < bookAdapter.getCount() && (bookAdapter.getItem(selection + 1) != null)){
bookAdapter.getItem(selection + 1).onPause();
}
fragment.setCurrentPage(true);
fragment.onResume();
}
};
public static class BookPagerAdapter extends FragmentStatePagerAdapter{
private List<Fragment> mBookPagesList;
private int mReadType;
public BookPagerAdapter(FragmentManager fm,int readType) {
super(fm);
// TODO Auto-generated constructor stub
mReadType = readType;
initPages();
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return mBookPagesList.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mBookPagesList.size();
}
private void initPages(){
mBookPagesList = new ArrayList<Fragment>();
mBookPagesList.add(FirstPageFragment.newInstance(mReadType));
mBookPagesList.add(SecondPageFragment.newInstance(mReadType));
mBookPagesList.add(ThirdPageFragment.newInstance(mReadType));
mBookPagesList.add(FourthPageFragment.newInstance(mReadType));
mBookPagesList.add(FifthPageFragment.newInstance(mReadType));
mBookPagesList.add(SixthPageFragment.newInstance(mReadType));
mBookPagesList.add(SeventhPageFragment.newInstance(mReadType));
mBookPagesList.add(EigthPageFragment.newInstance(mReadType));
mBookPagesList.add(NinethPageFragment.newInstance(mReadType));
mBookPagesList.add(TenthPageFragment.newInstance(mReadType));
mBookPagesList.add(EleventhPageFragment.newInstance(mReadType));
mBookPagesList.add(TwelvethPageFragment.newInstance(mReadType));
mBookPagesList.add(ThirteenthPageFragment.newInstance(mReadType));
mBookPagesList.add(FourteenthPageFragment.newInstance(mReadType));
mBookPagesList.add(FifteenthPageFragment.newInstance(mReadType));
mBookPagesList.add(SixteenthPageFragment.newInstance(mReadType));
mBookPagesList.add(SeventeenthPageFragment.newInstance(mReadType));
mBookPagesList.add(CreditsPageFragment.newInstance(mReadType));
}
}
}
BookPageFragment
public abstract class BookPageFragment extends Fragment{
public static final String TAG = BookPageFragment.class.getSimpleName();
public static final int AUTO_PLAY = 1;
public static final int READ_WITH_ME = 2;
public static final int READ_TO_MYSELF = 3;
private ViewPagerController mPageController;
protected final static String READ_TYPE_KEY = "readType";
private boolean mIsCurrentPage;
private int mCurrentWordIndex;
private MediaPlayer mMediaPlayer;
private BookPage mBookPage;
private SpeechController mSpeechController;
private TextHighlightController mCurrentHighlightController;
protected List<TextHighlightController> mHighlighters;
private int mCurrentHighlightIndex;
public abstract int getJsonId();
public abstract int getAudioResourceId();
protected abstract int[] getParagraphsTextView();
protected abstract int[] getParagraphsStringRes();
protected abstract int[] getPopupButtonsId();
public BookPageFragment(){
mIsCurrentPage = false;
mCurrentHighlightIndex = 0;
}
public void endAudioPlayback(){
if(getReadTypeFromArgs() == AUTO_PLAY){
mPageController.goToNextPage();
}
}
private void loadHighlighters(){
int[] paragraphsTextViews = getParagraphsTextView();
int[] stringRes = getParagraphsStringRes();
for(int i=0;i<paragraphsTextViews.length;i++){
int currentParagraph = paragraphsTextViews[i];
int stringId = stringRes[i];
TextView paragraph = (TextView)getView().findViewById(currentParagraph);
TextHighlightController controller = TextHighlightController.newInstance(getActivity(), paragraph, stringId);
addHighlighter(controller);
}
}
public final void highlightNextWord(){
if(!mCurrentHighlightController.highlightNextWord()){
mCurrentHighlightIndex++;
if(mCurrentHighlightIndex < mHighlighters.size()){
mCurrentHighlightController = mHighlighters.get(mCurrentHighlightIndex);
mCurrentHighlightController.highlightNextWord();
}
}
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.i(TAG,"onAttach");
try{
mPageController = (ViewPagerController)activity;
}catch(ClassCastException e){
throw new IllegalArgumentException("Activity not implementing ViewPagerController");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate");
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
resetAll();
Log.i(TAG,"onDetach");
}
public void goToNextPage(){
mPageController.goToNextPage();
}
public void goToPreviousPage(){
mPageController.goToPreviousPage();
}
public void setCurrentPage(boolean isCurrentPage){
mIsCurrentPage = isCurrentPage;
}
public boolean isCurrentPage(){
return mIsCurrentPage;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG,"onStart");
}
@Override
public void onResume() {
// TODO Auto-generated method stub
Log.i(TAG,"onResume");
super.onResume();
resetAll();
configureBookPage();
}
public final void startWithAutoPlay(){
disablePopupButtons();
initAudioPlayback();
}
public final void startWithRead(){
initAudioPlayback();
}
private long prevWordStartTime = 0;
public final long getNextWordStart(){
long wordStart=-1;
if(mCurrentWordIndex < mBookPage.getWordList().size()){
Word currentWord = mBookPage.getWordList().get(mCurrentWordIndex);
wordStart = currentWord.getStartTime() - prevWordStartTime;
wordStart = wordStart + currentWord.getWordLength() - 25;
prevWordStartTime = currentWord.getEndTime();
mCurrentWordIndex++;
}
return wordStart;
}
public final long getNextWordLength(){
long wordLength;
if(mCurrentWordIndex < mBookPage.getWordList().size()){
Word currentWord = mBookPage.getWordList().get(mCurrentWordIndex);
if(mCurrentWordIndex == 0){
wordLength= currentWord.getStartTime();
}else{
wordLength = currentWord.getWordLength();
}
mCurrentWordIndex++;
}else{
wordLength = -1;
}
return wordLength;
}
public final void startPlayback(){
mMediaPlayer = MediaPlayer.create(getActivity(), getAudioResourceId());
mMediaPlayer.start();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
Log.i(TAG,"onPause");
mIsCurrentPage = false;
if(mSpeechController != null){
mSpeechController.stopHighlighting();
}
if(mMediaPlayer != null){
try{
mMediaPlayer.stop();
mMediaPlayer.release();
}catch(Exception e){
}
}
resetHighlighters();
resetAll();
super.onPause();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG,"onStop");
}
protected void addHighlighter(TextHighlightController controller){
if(mHighlighters == null){
mHighlighters = new ArrayList<TextHighlightController>();
}
mHighlighters.add(controller);
}
private void resetHighlighters(){
if(mHighlighters != null){
for(TextHighlightController controller : mHighlighters){
controller.reset();
}
}
mCurrentHighlightIndex = 0;
mCurrentWordIndex = 0;
}
private void resetAll(){
mCurrentWordIndex = 0;
mCurrentHighlightIndex = 0;
if(mMediaPlayer != null){
try{
mMediaPlayer.reset();
}catch(Exception e){
}
}
if(mCurrentHighlightController != null){
mCurrentHighlightController.reset();
}
if(mSpeechController != null){
Message msg = mSpeechController.obtainMessage(SpeechController.END_AUDIO);
mSpeechController.sendMessage(msg);
}
if(mHighlighters != null){
for(TextHighlightController controller : mHighlighters){
controller.reset();
}
}
}
private void initAudioPlayback(){
if(getJsonId() != 0){
initHighlightersList();
loadJson();
mSpeechController = new SpeechController(this);
Message msg = mSpeechController.obtainMessage(SpeechController.START_AUTO_PLAY);
mSpeechController.sendMessageDelayed(msg, 1000);
}
}
private void disablePopupButtons(){
if(getView() != null){
int[] buttonsId = getPopupButtonsId();
for(int currentButtonId : buttonsId){
getView().findViewById(currentButtonId).setVisibility(View.INVISIBLE);
}
}
}
private void initHighlightersList(){
loadHighlighters();
if(mHighlighters != null)
mCurrentHighlightController = mHighlighters.get(mCurrentHighlightIndex);
}
private void loadJson(){
Gson gson = new Gson();
InputStream jsonStream = getResources().openRawResource(getJsonId());
InputStreamReader jsonReader = new InputStreamReader(jsonStream);
mBookPage = gson.fromJson(jsonReader, BookPage.class);
}
private void configureBookPage(){
int currentReadType = getReadTypeFromArgs();
Log.i(TAG,"GotReadType");
switch(currentReadType){
case AUTO_PLAY:
if(mIsCurrentPage){
startWithAutoPlay();
}
break;
case READ_WITH_ME:
if(mIsCurrentPage){
startWithRead();
}
break;
case READ_TO_MYSELF:
//Should do Nothing
break;
default:
throw new IllegalArgumentException("You must select one of the reading types of BookPageFragment");
}
}
protected int getReadTypeFromArgs(){
return getArguments().getInt(READ_TYPE_KEY);
}
LOGCAT
09-30 22:56:37.602: E/AndroidRuntime(7640): FATAL EXCEPTION: main
09-30 22:56:37.602: E/AndroidRuntime(7640): java.lang.NullPointerException
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.fragment.BookPageFragment.loadHighlighters(BookPageFragment.java:68)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.fragment.BookPageFragment.initHighlightersList(BookPageFragment.java:275)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.fragment.BookPageFragment.initAudioPlayback(BookPageFragment.java:257)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.fragment.BookPageFragment.startWithRead(BookPageFragment.java:149)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.fragment.BookPageFragment.configureBookPage(BookPageFragment.java:298)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.fragment.BookPageFragment.onResume(BookPageFragment.java:140)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.mobimanage.kagadventures.MainActivity$1.onPageSelected(MainActivity.java:136)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:567)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2008)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.View.dispatchTouchEvent(View.java:7253)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2215)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1458)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2163)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.View.dispatchPointerEvent(View.java:7433)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.os.MessageQueue.nativePollOnce(Native Method)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.os.MessageQueue.next(MessageQueue.java:125)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.os.Looper.loop(Looper.java:124)
09-30 22:56:37.602: E/AndroidRuntime(7640): at android.app.ActivityThread.main(ActivityThread.java:5227)
09-30 22:56:37.602: E/AndroidRuntime(7640): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 22:56:37.602: E/AndroidRuntime(7640): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-30 22:56:37.602: E/AndroidRuntime(7640): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-30 22:56:37.602: E/AndroidRuntime(7640): at dalvik.system.NativeStart.main(Native Method)
最佳答案
而不是使用 getItem 来获取当前页面使用
(BookPageFragment) mBookPagerAdapter.instantiateItem(mBookPager, mBookPager.getCurrentItem());
这将返回对实际具有 View 的现有 fragment 的引用。
关于android - ViewPager 中的 fragment 在 getView() 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19107612/
我有一个像这样的 fragment 栈 F1 -> F2 -> F3 -> F4 -> F5 现在我需要删除 F2、F3、F4 fragment 。 我需要如果我从 F5 fragment 按下后退按
我需要帮助来理解以下场景的工作原理以及如何实现结果。 我有一个名为 ShoppingCart 的类。它有一个名为 addItemsToShoppingCartFromPreviousOrder 的方法
我有一个包含 ViewPager 的 fragment 。这个 ViewPager 显然是由其他 Fragments 填充的。 我的问题是,ViewPager 中的 Fragment 是否有任何方法(
所以我正在实现一个 FragmentActivity 并尝试添加一个 fragment ,但是我遇到了多个问题。我以前做过这个,实际上我使用的代码与上一个项目(它工作的地方)相同,但由于某种原因它在这
Closed. This question needs details or clarity。它当前不接受答案。
我想将 Android X fragment (androidx.fragment.app.Fragment) 转换为 Android native fragment (android.app.Fra
假设我有一个包含 10 列文本类型 (20) 的 SQLite 表。 ListFragment 将从数据库中提取 4 列并使用 SimpleCursorAdapter 显示在列表中。 选择后,List
我有一个对话框 fragment ,我为延迟初始化创建了一个类。当我显示对话框时,它显示正常。但是,当我关闭对话框时,它崩溃的原因是: fragment 与 fragment 管理器无关。 我也尝试过
我想在 View 分页器更改为 fragment 时刷新该 fragment 。 package com.mcivisoft.rcbeam; import android.os.Bundle; imp
我有一个名为的 Activity MainActivity 我在容器 R.id.mainContainer 中添加了 fragment “BenefitFragment”。 在 BenefitFrag
所以我有这个 ClientListView,效果很好,可以显示客户,我可以单击一个客户并在右侧获取他们的详细信息(在我的第二个 fragment 中)。由此处的布局定义: 这很好用,但后来我
我试过这段代码,但点击第一个 fragment 中的按钮并没有改变第二个 fragment 中的字符串值。 这是第一个 fragment 的 kotlin 文件。它有两个按钮,点击它们可以更改字符串值
我有以下情况:我打开 fragment A 和目标,通过按钮的单击事件转到 fragment B。当我在 fragment B 中并点击后退按钮(为了返回 fragment A) 我想将一些参数传递给
我制作了一个 NavigationDrawer fragment ,其中包含 Home、Settings、Feedback 等项目。 home 项在点击 home 时应该打开,home 是在应用程序启
我正在一个接一个地替换 2 个 fragment ,两个 fragment 都有不同的选项菜单。当我替换第二个 fragment 时,它也显示第一个 fragment 的菜单。 setHasOptio
我有问题: android.app.Fragment$InstantiationException: Unable to instantiate fragment ${packageName}.${a
我正在研究 fragment 转换。当我用第二个 fragment 替换第一个 fragment 时,它出现在第一个 fragment 的下方。我希望它移动到第一个 fragment 之上。我该怎么做
我在抽屉导航里总共有 12 个 fragment 。每个 fragment 都有 volley 方法。每个 fragment 都显示自己的 Volley 响应,除了 position = 1 和 po
我在我的 Activity 中使用了两个 fragment 。最初我将向 Activity 添加一个 fragment 。然后在第一个 fragment 中使用监听器我想用第二个 fragment 替
我正在实现一个“fragments-101”程序,当相应的按钮被点击时我会替换一个 fragment 。然而,下面的事情发生了: 为什么会这样?为什么初始 fragment 没有被完全替换?MainA
我是一名优秀的程序员,十分优秀!