I am doing a registration and I chose to do it using Tabs because it is extensive (many fields) and involves photos, so I can put the information organized, it is the order of service registration, however I noticed that Tabs is 99 % to present data I did not find any example that shows its use in a registration (in my case in sqlite) and now with the screen made how to save / update this data that are in the tabs?
I think the logic would be to get the contents of the tabs (3), to save a button on the last tab? will any of you have done something similar and can you suggest a way?
I tried to evolve the routine and now I have more information and codes that can illustrate what has been done until then.
The logic used to register with Tabs is that there will be a "listener" in navigation, this will access the corresponding fragment in the viewPager and get the data for validation or persistence.
Below the listener in the parent activity, note that the process will be done by getting the fragment of the previous tab
private void setupTabListener() {
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
View customView = tab.getCustomView();
if (customView != null) {
ImageView icon = (ImageView) customView.findViewById(R.id.itemImage);
int tabIconColor = ContextCompat.getColor(getApplicationContext(), android.R.color.white);
icon.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
}
}
// Aba Anterior
@Override
public void onTabUnselected(TabLayout.Tab tab) {
Log.i(TAG, "onTabUnselected()" + tab.getPosition());
View customView = tab.getCustomView();
Boolean valido;
// Obter o fragment usando o adapter
if (tab.getPosition()==0){
VeiculoFragment fragment = (VeiculoFragment)
.customFragmentPageAdapter
.getItem(tab.getPosition());
// Na execução do método do fragment abaixo ocorre um erro
fragment.isValid();
}
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
}
The Error Occurs and I Captured the Stack
Process: br.com.tecnico, PID: 19927
java.lang.NullPointerException
at br.com.tecnico.fragment.VeiculoFragment.isValid(VeiculoFragment.java:143)
at br.com.tecnico.atividades.InstalacaoActivity$1.onTabUnselected(InstalacaoActivity.java:158)
at android.support.design.widget.TabLayout.dispatchTabUnselected(TabLayout.java:1170)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1153)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1426)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1536)
The line where the error occurs is in the fragment that was instantiated and called the isValid () method, the error occurs in if (edtNumero.getText ())
class VeiculoFragment extends BaseFragment {
@Override
public boolean isValid() {
// O Erro ocorre neste if abaixo
if (edtNumero.getText().toString().trim().equals("")){
//mostraSnackErro(getResources().getString(R.string.erro_frota_vazia));
return false;
}
if (edtModelo.getText().toString().trim().equals("")){
//mostraSnackErro(getResources().getString(R.string.erro_modelo_vazio));
return false;
}