我剛注意到如果我使用DepthPageTransformer()動畫,應用程序崩潰while screen screen portrait to landscape。
ViewPager(帶頁面轉換器動畫)在旋轉屏幕時崩潰
崩潰的原因是NullPointerException異常:(但它不是我的數組列表或object.already增加了reason.And已經添加調試results.if我不使用任何變壓器,應用效果非常好。)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at android.support.v4.view.ViewPager.getChildDrawingOrder(ViewPager.java:800
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3704)
at android.view.View.draw(View.java:17472)
at android.support.v4.view.ViewPager.draw(ViewPager.java:2415)
at android.view.View.updateDisplayListIfDirty(View.java:16464)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885)
at android.view.View.updateDisplayListIfDirty(View.java:16424)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:341)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:366)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3134)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2933)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2522)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
,這是第一誤差線
在android.support.v4.view.ViewPager.getChildDrawingOrder(ViewPager.java:800
@Override
protected int getChildDrawingOrder(int childCount, int i) {
final int index = mDrawingOrder == DRAW_ORDER_REVERSE ? childCount - 1 - i : i;
final int result =
((LayoutParams) mDrawingOrderedChildren.get(index).getLayoutParams()).childIndex;
return result;
}
所以這意味着mDrawingOrderedChildren是null.why它發生,我不知道......
我注意到調試:
如果我禁用/評論setPageTransformer(真,新DepthPageTransformer( )),應用程序運行良好。
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// if uncomment here,app crashes when rotating screen.
// viewPager.setPageTransformer(true,new DepthPageTransformer());
}
和DepthPageTransformer.java
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
view.bringToFront();
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter{
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return HomeFragment.newInstance("Home");
case 1:
return AFragment.newInstance("a");
case 2:
return BFragment.newInstance("b");
case 3:
return CFragment.newInstance("c");
default:
return HomeFragment.newInstance("home");
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "Home";
case 1: return "A";
case 2: return "B";
case 3: return "C";
}
return null;
}
@Override
public int getCount() {
return 4;
}
}
我認爲問題出在DepthPageTransformer,但我仍然不能建議搞清楚it.Thank你(和謝謝你downvoting和關閉請求而不讀整個問題。)
的可能的複製[什麼是空指針異常,怎麼解決呢?(http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bharatesh
是的,我發現它爲什麼會拋出NPE錯誤,並寫出這個原因「所以這意味着mDrawingOrderedChildren是null.Why發生,我不知道...「但無法弄清楚。爲什麼mdrawingordered兒童是空的,我不知道。謝謝你的投票,並且很好的回答!幫了我很多! – erginduran