I am trying to add a Listview inside a Viewpager Ragment and returns me the following error. Any suggestions?

Asked

Viewed 242 times

0

3-22 17:00:33.850 24857-24857/br.edu.br E/Androidruntime: FATAL EXCEPTION: main Process: br.edu.Unilab.intercampi, PID: 24857 java.lang.Nullpointerexception at android.widget.Arrayadapter.init(Arrayadapter.java:310) at android.widget.Arrayadapter.(Arrayadapter.java:128) at br.edu.Unilab.intercampi.Fragments.FragmentA.(Fragment.java:23) at br.edu.Unilab.intercampi.Adapters.MyFragmentPagerAdapter.getItem(Myfragmentpageradapter.java:25) at android.support.v4.app.Fragmentpageradapter.instantiateItem(Fragmentpageradapter.java:101) at android.support.v4.view.Viewpager.addNewItem(Viewpager.java:1034) at android.support.v4.view.Viewpager.populate(Viewpager.java:1182) at android.support.v4.view.Viewpager.populate(Viewpager.java:1116) at android.support.v4.view.Viewpager.onMeasure(Viewpager.java:1642) at android.view.View.Measure(View.java:16497) at android.widget.Relativelayout.measureChildHorizontal(Relativelayout.java:719) at android.widget.Relativelayout.onMeasure(Relativelayout.java:455) at android.view.View.Measure(View.java:16497) at android.view.Viewgroup.measureChildWithMargins(Viewgroup.java:5125) at android.support.design.widget.Coordinatorlayout.onMeasureChild(Coordinatorlayout.java:714) at android.support.design.widget.Headerscrollingviewbehavior.onMeasureChild(Headerscrollingviewbehavior.java:90) at android.support.design.widget.Appbarlayout$Scrollingviewbehavior.onMeasureChild(Appbarlayout.java:1391) at android.support.design.widget.Coordinatorlayout.onMeasure(Coordinatorlayout.java:784) at android.view.View.Measure(View.java:16497) at android.view.Viewgroup.measureChildWithMargins(Viewgroup.java:5125) at android.widget.Framelayout.onMeasure(Framelayout.java:310) at android.support.v7.widget.Contentframelayout.onMeasure(Contentframelayout.java:139) at android.view.View.Measure(View.java:16497) at android.view.Viewgroup.measureChildWithMargins(Viewgroup.java:5125) at android.widget.Linearlayout.measureChildBeforeLayout(Linearlayout.java:1404) at android.widget.Linearlayout.measureVertical(Linearlayout.java:695) at android.widget.Linearlayout.onMeasure(Linearlayout.java:588) at android.view.View.Measure(View.java:16497) at android.view.Viewgroup.measureChildWithMargins(Viewgroup.java:5125) at android.widget.Framelayout.onMeasure(Framelayout.java:310) at android.view.View.Measure(View.java:16497) at android.view.Viewgroup.measureChildWithMargins(Viewgroup.java:5125) at android.widget.Linearlayout.measureChildBeforeLayout(Linearlayout.java:1404)

The class q contains the ViewPager and the List Activity

public class FragmentA extends Fragment {

String [] Viagens = new String[] {"Primeira Viagem", "Segunda Viagem", "Terceira Viagem", "Quarta Viagem", "Quinta Viagem", "Sexta Viagem"};

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Viagens);

ListView listView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    listView = (ListView) container.findViewById(R.id.list_a);
    listView.setAdapter(arrayAdapter);

    return inflater.inflate(R.layout.fragment_a, container, false);
}


}

The Layout of the class

inserir a descrição da imagem aqui

The adapter

public class Myfragmentpageradapter extends Fragmentpageradapter {

private String [] mTabTitles;


public MyFragmentPagerAdapter(FragmentManager fm, String [] mTabTitles) {
    super(fm);
    this.mTabTitles = mTabTitles;
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            return new FragmentA();
        case 1:
            return new FragmentB();
        case 2:
            return new FragmentC();
        default:
            return null;

    }
}

@Override
public int getCount() {
    return this.mTabTitles.length;
}

@Override
public CharSequence getPageTitle(int position) {
    return this.mTabTitles[position];
}
 }

Main activity

public class HorarioActivity extends AppCompatActivity {

private TabLayout mTabLayout;
private ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_horario);

    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);

    mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),getResources().getStringArray(R.array.titles_tab)));
    mTabLayout.setupWithViewPager(mViewPager);

}



}
  • The Resource array has how many items?

  • Resource array has 3 items

1 answer

0


I haven’t examined your code very well, but it’s very likely to be in the line below.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Viagens);

The getActivity() method will return null by being called when the class a instantiated before the onAttach(Activity Activity) method is invoked by android.

Make the second change and try again.

public class FragmentA extends Fragment {

    String [] Viagens = new String[] {"Primeira Viagem", "Segunda Viagem", "Terceira Viagem", "Quarta Viagem", "Quinta Viagem", "Sexta Viagem"};

    ArrayAdapter<String> arrayAdapter;

    ListView listView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        listView = (ListView) container.findViewById(R.id.list_a);

        arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Viagens);
        listView.setAdapter(arrayAdapter);

        return inflater.inflate(R.layout.fragment_a, container, false);
    }


}

Calling the getActivity() method in the onCreateView method will no longer return null, because the onAttach method has already been called before.

Search the activit and Fragment lifecycle there are numerous articles on the internet.

  • I ended up using another method, but obg in the same

Browser other questions tagged

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