我想有一些我错过的东西,因为代码在大多数机器上运行正常.任何帮助将不胜感激.
这是调用堆栈.
java.lang.NullPointerExceptionandroID.support.v4.app.Fragment.setUserVisibleHint in Fragment.java on line 819androID.support.v4.app.FragmentPagerAdapter.setPrimaryItem in FragmentPagerAdapter.java on line 130androID.support.v4.vIEw.VIEwPager.populate in VIEwPager.java on line 1066androID.support.v4.vIEw.VIEwPager.populate in VIEwPager.java on line 914androID.support.v4.vIEw.VIEwPager.onMeasure in VIEwPager.java on line 1436androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.vIEw.VIEwGroup.measureChilDWithmargins in VIEwGroup.java on line 4924androID.Widget.linearLayout.measureChildBeforeLayout in linearLayout.java on line 1421androID.Widget.linearLayout.measureVertical in linearLayout.java on line 698androID.Widget.linearLayout.onMeasure in linearLayout.java on line 579androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.vIEw.VIEwGroup.measureChilDWithmargins in VIEwGroup.java on line 4924androID.Widget.FrameLayout.onMeasure in FrameLayout.java on line 315androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.support.v4.Widget.DrawerLayout.onMeasure in DrawerLayout.java on line 639androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.vIEw.VIEwGroup.measureChilDWithmargins in VIEwGroup.java on line 4924androID.Widget.FrameLayout.onMeasure in FrameLayout.java on line 315androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.vIEw.VIEwGroup.measureChilDWithmargins in VIEwGroup.java on line 4924androID.Widget.linearLayout.measureChildBeforeLayout in linearLayout.java on line 1421androID.Widget.linearLayout.measureVertical in linearLayout.java on line 698androID.Widget.linearLayout.onMeasure in linearLayout.java on line 579androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.vIEw.VIEwGroup.measureChilDWithmargins in VIEwGroup.java on line 4924androID.Widget.FrameLayout.onMeasure in FrameLayout.java on line 315com.androID.internal.policy.impl.PhoneWindow$DecorVIEw.onMeasure in PhoneWindow.java on line 2155androID.vIEw.VIEw.measure in VIEw.java on line 15323androID.vIEw.VIEwRootImpl.performMeasure in VIEwRootImpl.java on line 1854androID.vIEw.VIEwRootImpl.measureHIErarchy in VIEwRootImpl.java on line 1102androID.vIEw.VIEwRootImpl.performTraversals in VIEwRootImpl.java on line 1275androID.vIEw.VIEwRootImpl.doTraversal in VIEwRootImpl.java on line 1000androID.vIEw.VIEwRootImpl$TraversalRunnable.run in VIEwRootImpl.java on line 4218androID.vIEw.Choreographer$CallbackRecord.run in Choreographer.java on line 725androID.vIEw.Choreographer.doCallbacks in Choreographer.java on line 555androID.vIEw.Choreographer.doFrame in Choreographer.java on line 525androID.vIEw.Choreographer$FramedisplayEventReceiver.run in Choreographer.java on line 711androID.os.Handler.handleCallback in Handler.java on line 615androID.os.Handler.dispatchMessage in Handler.java on line 92androID.os.Looper.loop in Looper.java on line 137androID.app.ActivityThread.main in ActivityThread.java on line 4744java.lang.reflect.Method.invokeNative(Native Method)java.lang.reflect.Method.invoke in Method.java on line 511com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run in ZygoteInit.java on line 786com.androID.internal.os.ZygoteInit.main in ZygoteInit.java on line 553dalvik.system.NativeStart.main(Native Method)
首先,我在活动的OnResume函数中设置视图寻呼机.
private voID initialiseVIEwPager(){ mLoginFragment = new WeakReference<LoginFragment>(new LoginFragment()); Bundle loginBundle = new Bundle(); loginBundle.putInt("SpinnerIndex",HDMSliveSession.getInstance().getSpinnerposition()); loginBundle.putString("Username",HDMSliveSession.getInstance().getUsername()); loginBundle.putString("Password",HDMSliveSession.getInstance().getpassword()); loginBundle.putInt("Mode",HDMSliveSession.getInstance().getConnectionMode().ordinal()); loginBundle.putBoolean("LoggedIn",HDMSliveSession.getInstance().isLoggedIn()); loginBundle.putBoolean("Connected",HDMSliveSession.getInstance().isConnected()); loginBundle.putString("LoginResult",HDMSliveSession.getInstance().getLoginResult()); loginBundle.putString("System",HDMSliveSession.getInstance().getSystem()); loginBundle.putInt("code",HDMSliveSession.getInstance().getAccessCode()); loginBundle.putLong("bytesSent",mCurrentSB); loginBundle.putLong("bytesReceived",mCurrentRB); loginBundle.putLong("nbytesSent",mNCurrentSB); loginBundle.putLong("nbytesReceived",mNCurrentRB); loginBundle.putInt("appID",mApp.getApplicationInfo().uID); loginBundle.putString(mWebSocketAddresspreference,mConnect.getWebSocketURL()); loginBundle.putString(mAPIAddresspreference,mLogin.getliveServerURL()); loginBundle.putBoolean(mautoLoginPreference,mautoLoginEnabled); loginBundle.putBoolean(mautoConnectPreference,mLogin.isautoConnectEnabled()); loginBundle.putInt(mautoReconnectTimePreference,mConnect.getautoReconnectTime()); loginBundle.putInt(mMaxautoReconnectionAttemptsPreference,mConnect.getMaxautoReconnectionAttempts()); loginBundle.putInt(mPingResponseTimePreference,mConnect.getPingResponseTime()); loginBundle.putInt(mAutopingTimePreference,mConnect.getAutopingTime()); loginBundle.putInt(mCurrentPingPreference,mConnect.getCurrentPing()); loginBundle.putInt(mautoReconnectAttemptsPreference,mConnect.getautoReconnectAttempts()); loginBundle.putInt(mautoReconnectTotalAttemptsPreference,mConnect.getautoReconnectTotalAttempts()); loginBundle.putBoolean(mPlayListMessagePreference,messageSubscriptionContains(mPlayListMessage)); loginBundle.putBoolean(mAutopageSwapPreference,mAutopageSwap); loginBundle.putBoolean(mWifiCheckedPreference,mWifiChecked); loginBundle.putBoolean(mautoWebCheckedPreference,mConnect.isautoWebChecked()); loginBundle.putBoolean(mGatewayCheckedPreference,mGatewayChecked); loginBundle.putBoolean(mDHS1CheckedPreference,mDNS1Checked); loginBundle.putBoolean(mHDMSliveCheckedPreference,mHDMSliveChecked); loginBundle.putBoolean(mGoogleCheckedPreference,mGoogleChecked); loginBundle.putBoolean(mHDMSCheckedPreference,mHDMSChecked); loginBundle.putBoolean(mParrotCheckedPreference,mParrotChecked); loginBundle.putBoolean(mLocaliPCheckedPreference,mLocaliPChecked); loginBundle.putString(mLocaliPPreference,mLocaliP); loginBundle.putInt(mMaxImagesFromWebPreference,mMaxImagesFromWeb); loginBundle.putInt(mMaxPingAttemptsPreference,mConnect.getMaxPingAttempts()); loginBundle.putInt(mFailedPingsPreference,mConnect.getFailedPings()); loginBundle.putBoolean("autoLogin",mLogin.isautoLogin()); loginBundle.putBoolean("wasLoggedIn",HDMSliveSession.getInstance().wasLoggedIn()); mLoginFragment.get().setArguments(loginBundle); mBAUFragment = new WeakReference<BAUFragment>(new BAUFragment()); Bundle bauBundle = new Bundle(); bauBundle.putBoolean("jump",mJumpToCurrent); bauBundle.putInt("place",mBAUposition); bauBundle.putBoolean(mBAUExpandedPreference,mBAUExpanded); mBAUFragment.get().setArguments(bauBundle); mPlayerFragment = new WeakReference<PlayerFragment>(new PlayerFragment()); mListFragment = new WeakReference<ListFragment>(new ListFragment()); Bundle ListBundle = new Bundle(); ListBundle.putInt(mListModePreference,mListMode); mListFragment.get().setArguments(ListBundle); mSearchFragment = new WeakReference<SearchFragment>(new SearchFragment()); Bundle searchBundle = new Bundle(); searchBundle.putInt(mSearchModePreference,mSearchMode); searchBundle.putString("searchqueryA",mSearchTextA); searchBundle.putString("searchqueryS",mSearchTextS); searchBundle.putString("TitleLast",mTitleLast); searchBundle.putString("artistLast",mArtistLast); searchBundle.putString("ListLast",mListLast); searchBundle.putString("yearLast",mYearLast); searchBundle.putString("genreLast",mGenreLast); mSearchFragment.get().setArguments(searchBundle); mVIDeoFragment = new WeakReference<VIDeoFragment>(new VIDeoFragment()); Bundle vIDeoBundle = new Bundle(); vIDeoBundle.putInt(mVIDeoOutputPreference,mVIDeoOutput); mVIDeoFragment.get().setArguments(vIDeoBundle); List<Fragment> fragments = new Vector<Fragment>(); fragments.add(mVIDeoFragment.get()); fragments.add(mPlayerFragment.get()); fragments.add(mBAUFragment.get()); fragments.add(mListFragment.get()); fragments.add(mSearchFragment.get()); fragments.add(mLoginFragment.get()); mPagerAdapter = new VIEwPagerAdapter(getSupportFragmentManager(),fragments); mVIEwPager = (VIEwPager)findVIEwByID(R.ID.contentVIEwPager); mVIEwPager.setAdapter(mPagerAdapter); mVIEwPager.setonPage@R_404_1962@(this); mVIEwPager.setVisibility(VIEw.VISIBLE); ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); if (am.getMemoryClass() >= 32) mVIEwPager.setoffscreenPagelimit(Fragments.Max.ordinal()); mLastFragment = -1; Updatedisplay();}
除非是具有小内存堆的设备,否则所有碎片都将被保留.目前有6个片段,崩溃发生时只有4个内存.所以我知道碎片的数量不是问题.我确实尝试将视图寻呼机设置设置为默认值,唯一的区别是速度,因为应用程序需要在用户刷新时加载碎片.当应用程序暂停时,所有碎片都将被销毁.
@OverrIDeprotected voID onSaveInstanceState(Bundle outState){ FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); if (mLoginFragment.get() != null) ft.remove(mLoginFragment.get()); if (mPlayerFragment.get() != null) ft.remove(mPlayerFragment.get()); if (mBAUFragment.get() != null) ft.remove(mBAUFragment.get()); if (mListFragment.get() != null) ft.remove(mListFragment.get()); if (mSearchFragment.get() != null) ft.remove(mSearchFragment.get()); if (mVIDeoFragment.get() != null) ft.remove(mVIDeoFragment.get()); ft.commit(); mLoginFragment = new WeakReference<LoginFragment>(null); mPlayerFragment = new WeakReference<PlayerFragment>(null); mBAUFragment = new WeakReference<BAUFragment>(null); mListFragment = new WeakReference<ListFragment>(null); mSearchFragment = new WeakReference<SearchFragment>(null); mVIDeoFragment = new WeakReference<VIDeoFragment>(null); mPagerAdapter = null; mVIEwPager = null; mFragmentsLoaded = 0; mLastFragment = -1;}
这是我的观点寻呼机代码.
package com.hdms.manager.Fragments;/** * Created by bradj on 8/10/13. * */import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.app.FragmentPagerAdapter;import androID.support.v4.app.FragmentTransaction;import androID.vIEw.VIEw;import java.util.List;public class VIEwPagerAdapter extends FragmentPagerAdapter{ private final List<Fragment> mFragments; FragmentManager mFragmentManager; public VIEwPagerAdapter(FragmentManager aFragmentManager,List<Fragment> aFragments) { super(aFragmentManager); mFragmentManager = aFragmentManager; mFragments = aFragments; } @OverrIDe public Fragment getItem(int aposition) { return mFragments.get(aposition); } @OverrIDe public long getItemID(int aposition) { return aposition; } @OverrIDe public voID destroyItem(androID.vIEw.VIEwGroup aContainer,int aposition,java.lang.Object aObject) { if (aposition <= getCount() && aObject != null) { FragmentTransaction trans = mFragmentManager.beginTransaction(); trans.remove((Fragment) aObject); trans.commit(); } } @OverrIDe public int getCount() { return mFragments.size(); }}解决方法 最后!我现在能够可靠地重新创建这个错误!
要重新创建错误,关闭活动/应用程序,并快速重新打开页面与片段.您可能需要尝试几次,因为在我的测试中,我不得不在30秒内重新打开应用程序.不同速度设备的时间可能会更慢或更快.
问题是我只显式创建了Fragment(使用new)一次,并保留了对该实例的引用,以便我可以重用它.对此问题的一个简单的解决方案是始终返回Fragment的Fragment的新实例FragmentPagerAdapter.getItem(…),如下所示.
public class VIEwPagerAdapter extends FragmentPagerAdapter { ... @OverrIDe public Fragment getItem(int position) { switch (position) { case 0: return mMyFragment; // Error. Has the edge-case crash. case 1: return new MyFragment(); // Works. default: return new MyDefaultFragment(); } }}
对于OP的具体情况,使用列表< Fragment>持有引用可能与上述相同的问题案例.
ps – 根问题可能与Fragment生命周期有关,并尝试在销毁它时再次使用它.
pps – 另一种重新创建错误的方法是在足够的选项卡之间快速切换,以使Fragment想要被破坏,从缓存中释放一些内存,然后快速返回.默认情况下,FragmentPagerAdapter仅将一个片段缓存到“左”和“右”.因此,根据您的缓存限制,您必须至少有三个选项卡以这种方式重新创建错误.
ppps – 此解决方案修复了用于androID.app.Fragment.setUserVisibleHint(Fragment.java:997)的NullPointerException,并且也适用于androID.support.v4.app.Fragment.setUserVisibleHint.
总结以上是内存溢出为你收集整理的android.support.v4.app.Fragment.setUserVisibleHint应用程序恢复的空指针全部内容,希望文章能够帮你解决android.support.v4.app.Fragment.setUserVisibleHint应用程序恢复的空指针所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)