Locking app when adding a new page in Pageradapter

Asked

Viewed 31 times

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;
    }
}

2 answers

1


You are accessing an instantiated property in the Pageradapter class or your arraylist is another reference in memory and not the one you pass to your constructor

try the following

viewPager.setAdapter(new PagerAdapter(context, listCodePage)); viewPager.getAdapter().getData().add(addCodePage(namePage,textCode);

It also modifies its statistical method to return something or in this case its item HashMap<Object, Object>;

Using a static method to change an arraylist that is only instantiated by Class is not a good idea, probably what is happening is that your method is currently doing the following.

public static void addCodePage(String namePage, String textCode) {
    HashMap<Object, Object> item = new HashMap<>();
    item.put("name", namePage);
    item.put("code", textCode);
    null.add(item);
}

Always try to avoid side effects and make their methods return something, especially if they are static.

Try to do this.

public static HashMap<Object, Object> addCodePage(String namePage, String textCode) {
    HashMap<Object, Object> item = new HashMap<>();
    item.put("name", namePage);
    item.put("code", textCode);
    return item;
}

and use as follows on onCreate

litsCodePage.add(addCodePage("Tab1", ""));

I hope I’ve helped.

Also create a Getter on Pageradapter.

From what I also understood your Hashmap uses two Strings, you can be more specific and use

HashMap<String,String> 

Rather than:

HashMap<Object, Object>
  • 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)

0

Thanks, I reviewed my concepts of programming, but the application still crasha... I had to use another code, that presented the error Unknown method: getData or androidx.viewpager.widget.Pageradapter

What the new code looks like:

Add page and set Viewpager Adapter

listCodePage.add(addCodePage("Tab1", ""));
viewPager.setAdapter(new PagerAdapter(context, listCodePage));

Error free code I tried to do (still crashes the app)

PagerAdapter.getData().add(addCodePage("a", "b"));

Error code

viewPager.getAdapter().getData().add(addCodePage("a", "b"));

getData()

public class PagerAdapter extends PagerAdapter {

//...

    public static ArrayList<HashMap<String, String>> getData()
        {
             return data;
        }

//...

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.