我刚注意到如果我使用 DepthPageTransformer ()动画,应用程序会在将屏幕纵向旋转到横向时崩溃 。
崩溃原因是NullPointerException :(但它不是关于我的数组列表或object.already添加了一个原因。并且已经添加了调试结果。如果我不使用任何变换器,应用程序效果非常好。)
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。为什么会发生这种情况,我不知道......
我注意到调试:
如果我禁用/注释 setPageTransformer(true,new 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中,但我仍然无法弄清楚。感谢你的建议(并感谢你低估并关闭请求而不阅读整个问题。)
答案 0 :(得分:0)
只需在清单文件中添加此行
即可<activity
-----
-----
android:configChanges="orientation|screenSize" >
</activity>
hopw这对你有帮助