滑块来自onBackPressed

时间:2018-03-01 11:10:17

标签: android android-viewpager

我需要在包装器活动中按下后退按钮时以编程方式更改我的寻呼机的幻灯片,但是当我在onBackPressed中调用pager.setCurrentItem(1)时,应用程序正在关闭。

代码:

public class MainActivity extends FragmentActivity {

static final int NUM_ITEMS = 3;
static int fSelected = 1;

MyAdapter mAdapter;
ViewPager mPager;

// ADAPTER
public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return FirstFragment.init(position);
            case 1:
                return SecondFragment.init(position);
            case 2:
                return ThirdFragment.init(position);
            default:
                return null;
        }
    }

}


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Context mContext = getApplicationContext();
    playAppearAnimation(mContext);

    // ANIM
    playAppearAnimation(getApplicationContext());



    final ViewPager mPager = findViewById(R.id.pager);
    mAdapter = new MyAdapter(getSupportFragmentManager());
    mPager.setAdapter(mAdapter);

    mPager.setCurrentItem(1);


    //disabilita lo scroll by touch0
    mPager.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            return false;
        }
    });



    // BACK TO RETRIEVE PASSWORD
    TextView button = (TextView)findViewById(R.id.lost_password);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(0);
        }
    });
    /*
    button = (Button)findViewById(R.id.goto_last);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {+

            mPager.setCurrentItem(NUM_ITEMS-1);
        }
    });*/



    // ANIMAZIONE DEL RECUPERA PASSWORD
    mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        public void onPageScrollStateChanged(int state) {}
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        public void onPageSelected(int position) {
            // NASCONDO LOST PASSWORD
            View lostPassword = (TextView)findViewById(R.id.lost_password);
            //Toast.makeText(getApplicationContext(),""+position, Toast.LENGTH_LONG).show();
            //>> HIDE / UNHIDE lost password
            switch(position){
                case 0:
                    fSelected = 0;
                    lostPassword.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
                    break;
                case 1:
                    fSelected = 1;
                    lostPassword.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in));
                    break;
                case 2:
                    fSelected = 2;
                    lostPassword.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
                    break;
            }
        }
    });


}

public void playAppearAnimation(Context context){
    ImageView logo = findViewById(R.id.logo);
    ImageView logotype = findViewById(R.id.logotype);
    logo.startAnimation(AnimationUtils.loadAnimation(context, R.anim.logo_anim));
    logotype.startAnimation(AnimationUtils.loadAnimation(context, R.anim.logo_anim));

    com.ubris.design.test1.NonSwipeableViewPager pager = findViewById(R.id.pager);
    pager.startAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_fragments));
}


@Override
public void onBackPressed() {

    mPager.setCurrentItem(0);
}

logcat的:

  

java.lang.NullPointerException:尝试调用虚方法' void   android.support.v4.view.ViewPager.setCurrentItem(INT)'在null   对象参考                         在com.ubris.design.test1.MainActivity.onBackPressed(MainActivity.java:155)                         在android.app.Activity.onKeyUp(Activity.java:2826)                         在android.view.KeyEvent.dispatch(KeyEvent.java:2766)                         在android.app.Activity.dispatchKeyEvent(Activity.java:3146)                         在com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:353)                         在android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4742)                         在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4713)                         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4249)                         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4302)                         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4268)                         在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4395)                         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4276)                         在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4452)                         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4249)                         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4302)                         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4268)                         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4276)                         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4249)                         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4302)                         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4268)                         在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4428)                         在android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4589)                         在android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2512)                         在android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2106)                         在android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2097)                         在android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2487)                         在android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)                         在android.os.MessageQueue.nativePollOnce(本机方法)                         在android.os.MessageQueue.next(MessageQueue.java:356)                         在android.os.Looper.loop(Looper.java:138)                         在android.app.ActivityThread.main(ActivityThread.java:6523)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:942)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) italic

1 个答案:

答案 0 :(得分:2)

从代码中删除super.onBackPressed() - 在这种情况下通常会关闭应用。

UPD。:当您发布代码时,问题很明显。 将final ViewPager mPager = findViewById(R.id.pager);更改为mPager = findViewById(R.id.pager);以分配其值,而不是创建局部变量。