具有适配器" "的视图寻呼器需要视图 ID 在异步任务中



我有一个AsyncTask,我在其中创建了一个ViewPager,当我为其分配适配器时,会出现此错误

java.lang.IollegalStateException:带有适配器的ViewPagercom.example.kos.PagerAdapterZvon@646addd需要视图id位于androidx.fragment.app.FragmentPagerAdapter.startUpdate(FragmentPagedAdapter.java:83)位于androidx.viewpager.widget.viewpager.populate(viewpager.java:1124)位于androidx.viewpager.widget.viewpager.populate(viewpager.java:1092)位于androidx.viewpager.widget.viewpager.onMeasure(viewpager.java:1622)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)在android.widget.LinearLayout.measureVertical(LinearLayout.java:847)在android.widget.LinearLayout.onMeasure(LinearLayout.java:726)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在androidx.coordinatorlayout.widget.CordinatorLayout.onMeasureChild(coordinatorlayout.java:733)在androidx.coordinatorlayout.widget.CordinatorLayout.onMeasure(coordinatorlayout.java:805)在android.view.view.measure(view.java:23466)在androidx.drawerlayout.widget.drawerlayout.onMeasure(drawerlayout.java:1119)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)位于androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)在android.widget.LinearLayout.measureVertical(LinearLayout.java:847)在android.widget.LinearLayout.onMeasure(LinearLayout.java:726)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)在android.widget.LinearLayout.measureVertical(LinearLayout.java:847)在android.widget.LinearLayout.onMeasure(LinearLayout.java:726)在android.view.view.measure(view.java:23466)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)网址:com.android.internal.policy.DecorView.onMeasure(DecorView.java:847)在android.view.view.measure(view.java:23466)在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2954)在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1753)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2041)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1636)在android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7946)在android.view.Cchoreographer$CallbackRecord.run(Choreographer.java:1092)在android.view.Cchoreographer.doCallbacks(Choreographer.java:893)位于android.view.Cchoreographer.doFrame(Choreographer.java:812)在android.view.Cchoreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)在android.os.Handler.handleCallback(Handler.java:907)在android.os.Handler.dispatchMessage(Handler.java:105)在android.os.Looper.loop(Looper.java:216)在android.app.ActivityThread.main(ActivityThreads.java:7625)位于java.lang.reflect.Method.ioke(本机方法)网址:com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:987)

异步任务

class StartAsyncTask extends AsyncTask<Void,String[],Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
linearLayout.removeAllViews();
ProgressBar progressBar = new ProgressBar(context);
linearLayout.addView(progressBar, layoutParams);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LinearLayout.LayoutParams layoutParamsKek = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParamsKek.gravity = Gravity.CENTER;
linearLayout.removeAllViews();
ViewPager viewPager = new ViewPager(context);
viewPager.setAdapter(pagerAdapter);
linearLayout.addView(viewPager,layoutParamsKek);
//            Date start = new Date();
//            switch (start.toString().substring(0,3)) {
//                case "Mon":
//                    viewPager.setCurrentItem(0);
//                    url = "Monday.txt";
//                    break;
//                case "Tue":
//                    viewPager.setCurrentItem(1);
//                    url = "Tuesday.txt";
//                    break;
//                case "Wed":
//                    viewPager.setCurrentItem(2);
//                    url = "Wednesday.txt";
//                    break;
//                case "Thu":
//                    viewPager.setCurrentItem(3);
//                    url = "Thursday.txt";
//                    break;
//                case "Fri":
//                    viewPager.setCurrentItem(4);
//                    url = "Friday.txt";
//                    break;
//                case "Sat":
//                    viewPager.setCurrentItem(5);
//                    url = "Saturday.txt";
//                    break;
//                default:
//                    viewPager.setCurrentItem(0);
//                    url = "Monday.txt";
//                    break;
//            }
////            SharedPreferences.Editor editor = settings.edit();
////            editor.putString("Day", url);
////            editor.apply();
////            tabLayout.setupWithViewPager(viewPager);
////            tabLayout.setScrollX(tabLayout.getWidth());
////            tabLayout.getTabAt(5).select();
}
@Override
protected Void doInBackground(Void... voids) {
pagerAdapter = new PagerAdapterZvon(getActivity().getSupportFragmentManager());
return null;
}
}

适配器

class PagerAdapterZvon extends FragmentPagerAdapter {
private String[] name;
PagerAdapterZvon(@NonNull FragmentManager fm) {
super(fm);
name = new String[] {
"Понедельник",
"Вторник",
"Среда",
"Четверг",
"Пятница",
"Суббота"
};
}
@Override
public CharSequence getPageTitle(int position){
return name[position];
}
@Override
public int getCount() {
return 6;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new classMonday();
case 1:
return new classTuesday();
case 2:
return new classWednesday();
case 3:
return new classThursday();
case 4:
return new classFriday();
case 5:
return new classSaturday();
default:
return null;
}
}
}

所有操作都发生在一个片段中,如果删除AsyncTask并将所有操作转移到onCreate,则一切正常

只需调用setId(R.id.your_id);

viewPager.setId(R.id.viewPagerId);

您可以通过按OPTION+ENTER(由于未解析的id而存在错误)并单击"创建新id值"来创建R.id id。

您似乎完全是从代码创建Viewpager,而不是通过布局文件创建的,这就是为什么您会出现错误(需要视图id)。创建视图寻呼机时,请按照此处的说明进行操作:

带有FragmentPagerAdapter 的ViewPager

相关内容

  • 没有找到相关文章

最新更新