片段android的缓慢创建
Posted
技术标签:
【中文标题】片段android的缓慢创建【英文标题】:slow creation of fragment android 【发布时间】:2021-01-30 02:43:58 【问题描述】:所以我有一个片段启动非常缓慢,就像跳过 150 多帧一样。 我正在尝试在片段内设置 ViewPager 并添加 onPageChangeListener ,它会在向某个方向滑动后禁用滑动。
问题在于它确实减慢了片段加载速度。我尝试将其移至单独的线程和 AsyncTask,但它仍然很慢。
片段:
public class SettingsFragment extends Fragment
public View mView;
public SettingsFragment()
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Deprecated
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
final View rootview = inflater.inflate(R.layout.fragment_settings,container,false);
this.mView=rootview;
Task task=new Task();
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return rootview;
@Deprecated
public class Task extends AsyncTask<Void,Void,Void>
@Override
protected Void doInBackground(Void... voids)
final CustomViewPager viewPager = mView.findViewById(R.id.ViewPager);
viewPager.setAdapter(new Adapter(getFragmentManager()));
viewPager.setCurrentItem(1);
viewPager.setOffscreenPageLimit(1);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
if (position == 0)
viewPager.setPagingEnabled(false);
@Override
public void onPageScrollStateChanged(int state)
);
return null;
片段的xml布局文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#fff" android:animationCache="true"
android:id="@+id/SettingsFragmentContainer"
android:drawingCacheQuality="high">
<androidx.appcompat.widget.Toolbar
android:layout_
android:layout_
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/SettingsActivityToolbar" app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:text="Settings"
android:layout_
android:layout_ app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="18dp"
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/Settings_textview"
android:textAppearance="@style/ActionBar.nameText"/>
<ImageButton
android:layout_
android:layout_ app:srcCompat="@drawable/settings_icon_back"
android:id="@+id/backPressButton"
android:rotation="180" android:backgroundTint="#fff"
android:background="@null"
app:layout_constraintStart_toStartOf="@+id/SettingsActivityToolbar"
android:layout_marginStart="2dp" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp" android:onClick="click" android:drawingCacheQuality="high"/>
<TextView
android:text=""
android:layout_
android:layout_ app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/linebreak1" android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@+id/backPressButton" android:background="#A1A8AC"/>
<Switch
android:text="Test Test"
android:layout_
android:layout_ android:id="@+id/switch4"
android:textAppearance="@style/ActionBar.nameText" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="25dp" android:layout_marginEnd="25dp"
app:layout_constraintEnd_toEndOf="parent" android:thumbTint="@drawable/switch_thumb_selector"
android:trackTint="@drawable/switch_track_selector" android:layout_marginTop="18dp"
app:layout_constraintTop_toBottomOf="@+id/backPressButton" tools:ignore="UseSwitchCompatOrMaterialXml"/>
<TextView
android:layout_
android:layout_ android:id="@+id/linebreak2"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="25dp" android:layout_marginEnd="25dp"
app:layout_constraintEnd_toEndOf="parent" android:background="#A1A8AC"
android:layout_marginTop="10dp" app:layout_constraintTop_toBottomOf="@+id/switch4"/>
<TextView
android:layout_
android:layout_ android:id="@+id/update"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:clickable="true"
android:textAppearance="@style/ActionBar.nameText" app:layout_constraintHorizontal_bias="0.0"
android:background="?attr/selectableItemBackground" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/switch4" android:onClick="onClickUpdate"/>
<TextView
android:text="Current Version 1.0.1"
android:layout_
android:clickable="true"
android:layout_ android:id="@+id/enter_version"
app:layout_constraintTop_toTopOf="@+id/update"
android:layout_marginTop="36dp" android:layout_marginEnd="206dp"
app:layout_constraintEnd_toEndOf="@+id/update"/>
<TextView
android:text="Check Update"
android:layout_
android:layout_
android:clickable="true"
android:id="@+id/update_textview"
android:textAppearance="@style/ActionBar.nameText" android:layout_marginEnd="228dp"
app:layout_constraintEnd_toEndOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"
android:layout_marginTop="10dp"/>
<com.testapp.test2.CustomViewPager
android:layout_
android:layout_
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/ViewPager" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter.java
public class Adapter extends FragmentStatePagerAdapter
public Adapter(FragmentManager fragmentManager)
super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
@NonNull
@NotNull
@Override
public Fragment getItem(int position)
if(position==0)
return new BlankFragment();
if(position==1)
return new SettingsFragment();
return null;
@Override
public int getCount()
return 2;
Logcat:
2020-10-15 07:51:34.234 14029-14034/com.testapp.test2 I/zygote64: Increasing code cache capacity to 1024KB
2020-10-15 07:51:35.302 14029-14029/com.testapp.test2 I/Choreographer: Skipped 165 frames! The application may be doing too much work on its main thread.
2020-10-15 07:51:50.250 14029-14034/com.testapp.test2 I/zygote64: Do full code cache collection, code=502KB, data=324KB
2020-10-15 07:51:50.252 14029-14034/com.testapp.test2 I/zygote64: After code cache collection, code=501KB, data=287KB
2020-10-15 07:51:51.006 14029-14034/com.testapp.test2 I/zygote64: Do partial code cache collection, code=503KB, data=297KB
2020-10-15 07:51:51.007 14029-14034/com.testapp.test2 I/zygote64: After code cache collection, code=503KB, data=297KB
2020-10-15 07:51:51.007 14029-14034/com.testapp.test2 I/zygote64: Increasing code cache capacity to 2MB
2020-10-15 07:51:51.886 14029-14034/com.testapp.test2 I/zygote64: Compiler allocated 8MB to compile void android.view.ViewRootImpl.performTraversals()
2020-10-15 07:51:54.868 14029-14029/com.testapp.test2 I/TouchMotionHandler: handler onDetached
我该如何解决这个问题?
编辑:
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.fragment.app.Fragment.setMenuVisibility(boolean)' on a null object reference
at androidx.fragment.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:189)
at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1158)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:22251)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:763)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:426)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:335)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:117)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1532)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1607)
at android.view.View.measure(View.java:22251)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:763)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:426)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:105)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:247)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:117)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1532)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1607)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22251)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6767)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:898)
at android.view.View.measure(View.java:22251)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2500)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1564)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1826)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1446)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6954)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
at android.view.Choreographer.doCallbacks(Choreographer.java:737)
at android.view.Choreographer.doFrame(Choreographer.java:669)
2020-10-16 00:56:36.824 14663-14663/com.testapp.test2 E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:911)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
【问题讨论】:
我读这篇文章的方式是你有一个SettingsFragement
,它显示一个ViewPager
,它加载与第2页相同的SettingsFragement
,其中包含相同ViewPager
的另一个实例,在永无止境的循环,这会使它变慢。为什么SettingsFragement
会以这种方式递归?
如果我从适配器中删除SettingsFragment
,我会得到空指针异常。我在上面添加了logcat。帮助我@Andrew..
您是如何删除SettingsFragment
的?您刚刚注释掉了return new SettingsFragment();
行吗?如果这样做肯定会生成 NullPointerException,除非您还将 getCount()
返回更改为 1
或者您可以将该行替换为 return new BlankFragment();
我确实将 getCount()
更改为 1,但它仍然给我 NullPointerException。 @安德鲁
用return new BlankFragment()
替换return new SettingsFragment()
会创建另一个BlankFragment
,因为我在SettingsFragment
Andrew的OnCreateView中设置了viewPager.setCurrentItem(1)
【参考方案1】:
我通过将构建类型从调试更改为发布来修复它。
【讨论】:
以上是关于片段android的缓慢创建的主要内容,如果未能解决你的问题,请参考以下文章