我的APP中有导航抽屉。我想在导航抽屉标题处创建幻灯片。因此,我使用ViewFlipper作为nav_header.xml。我在MainActivity中使用了以下代码。但应用程序停止了。我应该做些什么改变?
出现此错误:
/home/phrogz/Qt5.7.0
这是我的代码:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myapp.praxi/com.myapp.praxi.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
nav_header文件:
int[] resources = {R.drawable.prxi,
R.drawable.prxi1,
R.drawable.prxi2,
R.drawable.prxi3,
};
ViewFlipper imageFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
for (int i = 0; i < resources.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(resources[i]);
imageFlipper.addView(imageView);
}
imageFlipper.setFlipInterval(4000);
imageFlipper.setInAnimation(this, android.R.anim.slide_in_left); //use either the default slide animation in sdk or create your own ones
imageFlipper.setOutAnimation(this, android.R.anim.slide_out_right);
imageFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
int displayedChild = imageFlipper.getDisplayedChild();
int childCount = imageFlipper.getChildCount();
if (displayedChild == childCount - 1) {
imageFlipper.stopFlipping();
}
}
});
imageFlipper.startFlipping();`
&#13;
答案 0 :(得分:0)
ViewFlipper
是抽屉布局元素的子元素。您需要从导航抽屉的布局中找到视图,而不是从Activity
的视图布局中找到。
例如,如果这是您的抽屉布局,您必须像这样实例化ViewFlipper
:
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ViewFlipper imageFlipper = (ViewFlipper) mDrawerLayout.findViewById(R.id.view_flipper);
希望有所帮助。