如何修复ViewPager以防止应用程序出错并崩溃?

时间:2018-08-29 06:38:47

标签: java android android-viewpager

我正在基于Androidhive教程在登录页面上工作,问题是Viewpager可以在2种布局下正常工作,但对于 3种布局应用会崩溃
 在androidhive教程中,使用了2种布局,但是我想使用3种,因为我先收到短信然后otp然后注册,所以问题出在第三种布局上,它不会出现并崩溃。

Androidhive教程链接: Part 1 Part 2

以下是代码:

XML         

    <LinearLayout
        android:id="@+id/layout_phone_registration"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="25dp"
            android:layout_marginTop="100dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="25dp"
            android:gravity="center_horizontal"
            android:inputType="textCapWords"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:text="@string/msg_enter_mobile"
            android:textColor="@android:color/white"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <EditText
                android:id="@+id/inputMobile"
                android:layout_width="240dp"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:fontFamily="sans-serif-light"
                android:hint="@string/lbl_mobile"
                android:inputType="phone"
                android:maxLength="12"
                android:padding="5dp"
                android:textColor="@color/colorPrimary"
                android:textCursorDrawable="@null"
                android:textSize="18dp" />

            <Button
                android:id="@+id/btn_request_sms"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="25dp"
                android:background="@color/colorPrimaryDark"
                android:text="@string/lbl_next"
                android:textColor="@android:color/white"
                android:textSize="14dp" />
        </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_otp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/colorPrimary"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="25dp"
            android:layout_marginTop="100dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:gravity="center_horizontal"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:text="@string/msg_sit_back"
            android:textColor="@android:color/white"
            android:textSize="16dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="25dp"
            android:gravity="center_horizontal"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:text="@string/msg_manual_otp"
            android:textColor="@android:color/white"
            android:textSize="12dp" />

        <EditText
            android:id="@+id/inputOtp"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:fontFamily="sans-serif-light"
            android:gravity="center_horizontal"
            android:hint="@string/lbl_enter_otp"
            android:inputType="number"
            android:maxLength="6"
            android:padding="10dp"
            android:textCursorDrawable="@null"
            android:textSize="18dp" />

        <Button
            android:id="@+id/btn_verify_otp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="25dp"
            android:background="@color/colorPrimaryDark"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:text="@string/lbl_submit"
            android:textColor="@android:color/white"
            android:textSize="14dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_complete_registration"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:gravity="center_horizontal"
        android:orientation="vertical">


            <EditText
                android:id="@+id/inputName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:background="@android:color/white"
                android:fontFamily="sans-serif-light"
                android:hint="@string/lbl_name"
                android:padding="5dp"
                android:singleLine="true"
                android:textColor="@color/colorPrimary"
                android:textSize="18dp" />

            <EditText
                android:id="@+id/inputAddress"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:background="@android:color/white"
                android:fontFamily="sans-serif-light"
                android:hint="@string/lbl_email"
                android:padding="5dp"
                android:textColor="@color/colorPrimary"
                android:textSize="18dp" />


            <Button
                android:id="@+id/btn_register_user"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="25dp"
                android:background="@color/colorPrimaryDark"
                android:text="@string/lbl_next"
                android:textColor="@android:color/white"
                android:textSize="14dp" />


    </LinearLayout>

</ir.atlaspio.atlasdrinkingservice.AdvancedUI.MyViewPager>>


<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginBottom="60dp"
    android:indeterminateTint="@color/colorAccent"
    android:indeterminateTintMode="src_atop"
    android:visibility="gone" />

<LinearLayout
    android:id="@+id/layout_edit_mobile"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txt_edit_mobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="16dp" />

    <ImageButton
        android:id="@+id/btn_edit_mobile"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:background="@null"
        android:src="@drawable/ic_exit_to_app_black_24dp" />
</LinearLayout>

Java

private ViewPager viewPager;
private ViewPagerAdapter adapter;
OnCreate =>
viewPager.setCurrentItem(0);


    adapter = new ViewPagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
Public Class Activity =>
class ViewPagerAdapter extends PagerAdapter {

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }

    public Object instantiateItem(View collection, int position) {

        int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.layout_phone_registration;
                break;
            case 1:
                resId = R.id.layout_otp;
                break;
            case 2:
                resId = R.id.layout_complete_registration;
                break;
        }
        return findViewById(resId);
    }
}

Logcat

  

08-29 10:48:12.426 4499-4499 / ir.atlaspio.atlasdrinkingservice
  E / InputEventSender:异常分派完成的信号。       08-29 10:48:12.426 4499-4499 / ir.atlaspio.atlasdrinkingservice E / MessageQueue-JNI:MessageQueue回调中的异常:   handleReceiveCallback       08-29 10:48:12.436 4499-4499 / ir.atlaspio.atlasdrinkingservice E / MessageQueue-JNI:java.lang.UnsupportedOperationException:必需   方法destroyItem没有被覆盖           在android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:201)           在android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:128)           在android.support.v4.view.ViewPager.populate(ViewPager.java:1172)           在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)           在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)           在android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617)           在android.support.v4.view.ViewPager.pageRight(ViewPager.java:2888)           在android.support.v4.view.ViewPager.arrowScroll(ViewPager.java:2844)           在android.support.v4.view.ViewPager.executeKeyEvent(ViewPager.java:2764)           在android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2738)           在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)           在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)           在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)           在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)           在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)           在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)           在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)           在com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)           在android.app.Activity.dispatchKeyEvent(Activity.java:2418)           在android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)           在android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)           在android.support.v7.app.AppCompatDelegateImplBase $ AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)           在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1962)           在android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3876)           在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3850)           在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3423)           在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3473)           在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3442)           在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3549)           在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3450)           在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3606)           在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3423)           在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3473)           在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3442)           在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3450)           在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3423)           在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3473)           在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3442)           在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3582)           在android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3742)           在android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2010)           在android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)           在android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)           在android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)           在android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)           在android.os.MessageQueue.nativePollOnce(本地方法)           在android.os.MessageQueue.next(MessageQueue.java:138)           在android.os.Looper.loop(Looper.java:123)           在android.app.ActivityThread.main(ActivityThread.java:5019)           在java.lang.reflect.Method.invokeNative(本机方法)         在java.lang.reflect.Method.invoke(Method。

2 个答案:

答案 0 :(得分:1)

根据您的错误

  

java.lang.UnsupportedOperationException:必需的方法destroyItem没有被覆盖

您需要在寻呼机适配器中添加此方法

@Override
void destroyItem (ViewGroup container, int position, Object object){
    ((ViewPager) container).removeView((View) object);
    // more code if needed
}

答案 1 :(得分:1)

在您的destroyItem类中覆盖ViewPagerAdapter方法,并将obj删除为container.removeView(obj as LinearLayout),其中LinearLayout是您的根布局

@Override 
void destroyItem(ViewGroup container, int position, Object obj) {
            container.removeView(obj as LinearLayout)
        }