Raj
Raj

Reputation: 1872

Application Crash ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup in ViewPager

I am having a ViewPager, with 4 pages. All the fragments content is created dynamically which contains some TextView's and ImageView. all the pages are created fine. But, if i try to swipe the page by touching on the TextView, Application crashed with below logs in Samsung Galaxy S4 - OS V4.4.2 alone.

I have tested in LG Nexus5 - OS V5.1 and Samsung Galaxy NoteII - OS V4.3 also. App working fine even when i touch on TextView and swipe the page.

Exception in MessageQueue callback: handleReceiveCallback
 java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
 at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
 at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
 at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
 at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
 at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
 at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1849)
 at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1984)
 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2043)

Below is My fragment code.

public class HorizontalSwipPage extends Fragment {

private View mPage = null;
LinearLayout root = null;

public void setArguments(View page) {
    mPage = page;
}

public HorizontalSwipPage() {
    super();
}

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

    root = null;
    root = new LinearLayout(getActivity());
    root.removeAllViews();
    root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.MATCH_PARENT));
    root.addView(mPage);

    return root;
}

@Override
public void onDestroyView() {
    // TODO Auto-generated method stub
    root.removeAllViews();
    root = null;
    super.onDestroyView();
}

}

here mPage is a dynamically created layout View. below is my adapter code.

public class TestPagerAdapter extends FragmentStatePagerAdapter implements
    IconPagerAdapter {

public int noOfPages = 0;

private View[] PageList = null;



public TestPagerAdapter(Context context, FragmentManager fm,
        int pNoOfPages) {
    super(fm);
    noOfPages = pNoOfPages;
    PageList = new View[noOfPages];

}

public void addPage(View grid, int index) {
    PageList[index] = grid;
}

@Override
public Fragment getItem(int position) {
    HorizontalSwipPage f = new HorizontalSwipPage();
    f.setArguments(PageList[position]);
    return f;
}

@Override
public int getCount() {
    return noOfPages;
}
}

Please some one help me to resolve this issue.

Upvotes: 0

Views: 595

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup

This is an Exception that can occur in a Java program when you try to improperly convert a class from one type to another.

I think you are missing to set TextView Proper Id .

Upvotes: 2

Related Questions