0
I’m creating a Viewpager using a Pageradapter, and a Tablayout for the pages, it creates pages normally, but when I try to create a new page after setting the Adapter from Viewpager, the Crasha app, does anyone know why? (Pages are created using an Arraylist<>, the list compliance sets the number of pages in Viewpager)
Oncreate
//...
public static TabLayout tabLayout;
public static DrawerLayout drawerLayout;
public static ViewPager viewPager;
public static Context context;
public static ArrayList<HashMap<Object,Object>> listCodePage = new ArrayList<>();
//...OnCreate
tabLayout = findViewById(R.id.tablayout);
drawerLayout = findViewById(R.id.drawer_layout);
viewPager = findViewById(R.id.viewpager);
context = getBaseContext();
//configura as tab
tabLayout.setupWithViewPager(viewPager);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
//Adicionando páginas para a ViewPager (Um titulo, e um texto)
addCodePage("Tab1", "");
addCodePage("Tab2", "");
addCodePage("Tab3", "");
addCodePage("Tab4", "");
viewPager.setAdapter(new PagerAdapter(context, listCodePage));
//<ERRO>
addCodePage("Tab5", "");
//Quando tento criar uma página depois do Adapter ser definido, o aplicativo crasha
addCodePage (Method to add pages)
public static void addCodePage(String namePage, String textCode) {
HashMap<Object, Object> item = new HashMap<>();
item.put("name", namePage);
item.put("code", textCode);
listCodePage.add(item);
}
Pageradapter
public class PagerAdapter extends PagerAdapter {
private ArrayList<HashMap<Object,Object>> data = new ArrayList<HashMap<Object,Object>>();
private Context context;
public PagerAdapter(Context context, ArrayList<HashMap<Object,Object>> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
@Override
public CharSequence getPageTitle(int position) {
return data.get(position).get("name").toString();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(context).inflate(R.layout.pager_main, container, false);
EditText editCode = view.findViewById(R.id.code);
editCode.setText(data.get(position).get("code").toString());
container.addView(view);
return view;
}
}
It helped me a lot, but the plobema hasn’t been solved yet, the application still crasha, I use AIDE to progamar, so I don’t have support for Logcat, I’m using the Viewpager of androidx-v1.0.0, and testing on a device with sdk 29 (android 10)
– Laxeder