gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:57:53 25 4
gpt4 key购买 nike

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

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: Exception dispatching finished signal. 08-29 10:48:12.426 4499-4499/ir.atlaspio.atlasdrinkingservice E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback 08-29 10:48:12.436 4499-4499/ir.atlaspio.atlasdrinkingservice E/MessageQueue-JNI: java.lang.UnsupportedOperationException: Required method destroyItem was not overridden at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:201) at android.support.v4.view.PagerAdapter.destroyItem(PagerAdapter.java:128) at android.support.v4.view.ViewPager.populate(ViewPager.java:1172) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617) at android.support.v4.view.ViewPager.pageRight(ViewPager.java:2888) at android.support.v4.view.ViewPager.arrowScroll(ViewPager.java:2844) at android.support.v4.view.ViewPager.executeKeyEvent(ViewPager.java:2764) at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2738) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035) at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505) at android.app.Activity.dispatchKeyEvent(Activity.java:2418) at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58) at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962) at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3876) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3850) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3549) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3606) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3582) at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3742) at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010) at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704) at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695) at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987) at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:138) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:5019) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.

最佳答案

根据您的错误

java.lang.UnsupportedOperationException: Required method destroyItem was not overridden

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

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

关于java - 如何修复 ViewPager 以防止应用程序出错和崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com