将ViewPager对象传递给不同的活动

时间:2017-11-05 23:32:26

标签: android android-viewpager android-pageradapter

我是Android开发的新手,所以我使用ViewPager和PagerAdapter创建了动态视图。我在ViewPagerHandler类中处理这个逻辑。这从活动A开始。

现在我想从不同的Activity B添加活动A中显示的新视图。我需要访问PagerAdapter对象以将视图添加到我的arraylist并将该视图设置为currentview。

我尝试将ViewPagerHandler对象传递给Activity B的intent,但是在下面的堆栈跟踪中失败了。

 java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.mak.test.weatherapp.ViewPagerHandler)
                                                                             at android.os.Parcel.writeSerializable(Parcel.java:1761)
                                                                             at android.os.Parcel.writeValue(Parcel.java:1709)
                                                                             at android.os.Parcel.writeArrayMapInternal(Parcel.java:777)
                                                                             at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)
                                                                             at android.os.Bundle.writeToParcel(Bundle.java:1181)
                                                                             at android.os.Parcel.writeBundle(Parcel.java:817)
                                                                             at android.content.Intent.writeToParcel(Intent.java:9480)
                                                                             at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4340)
                                                                             at android.app.Instrumentation.execStartActivity(Instrumentation.java:1610)
                                                                             at android.app.Activity.startActivityForResult(Activity.java:4472)
                                                                             at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                                                                             at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                                                                             at android.app.Activity.startActivityForResult(Activity.java:4430)
                                                                             at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
                                                                             at android.app.Activity.startActivity(Activity.java:4791)
                                                                             at android.app.Activity.startActivity(Activity.java:4759)
                                                                             at com.sjsu.cmpe277.weatherapp.MainActivity.onOptionsItemSelected(MainActivity.java:179)
                                                                             at android.app.Activity.onMenuItemSelected(Activity.java:3435)
                                                                             at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:368)
                                                                             at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                                                                             at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                                                                             at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:674)
                                                                             at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                             at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                                                                             at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                                                                             at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
                                                                             at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
                                                                             at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
                                                                             at android.view.View.performClick(View.java:6256)
                                                                             at android.view.View$PerformClick.run(View.java:24697)
                                                                             at android.os.Handler.handleCallback(Handler.java:789)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                             at android.os.Looper.loop(Looper.java:164)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                          Caused by: java.io.NotSerializableException: com.mak.test.weatherapp.MainActivity
                                                                             at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1233)
                                                                             at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1597)
                                                                             at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1558)
                                                                             at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1481)
                                                                             at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
                                                                             at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
                                                                             at android.os.Parcel.writeSerializable(Parcel.java:1756)

我已经检查了各种帖子,并建议在你的类中实现serializable,我已经将Serializable实现到我使用过的所有类,但仍然会出现此错误。

有没有办法可以从不同的活动中传递对PagerAdapter Object的访问权限并将其添加到其中。

1 个答案:

答案 0 :(得分:0)

您是否尝试使用Parceler

这是一个非常棒的图书馆,可以使用Parcel通过活动和片段发送海关对象