我有一個ViewPager,有4頁。所有的片段內容都是動態創建的,其中包含一些TextView和ImageView。所有的頁面都創建好了。但是,如果我嘗試通過觸摸TextView來輕掃頁面,則應用程序會在三星Galaxy S4 - OS V4.4.2中單獨以下日誌崩潰。應用崩潰ClassCastException:android.widget.TextView無法在ViewPager中轉換爲android.view.ViewGroup
我已經測試過LG Nexus5 - OS V5.1和三星Galaxy NoteII - OS V4.3。應用程序工作正常,即使當我觸摸TextView和刷卡頁面。
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)
以下是我的片段代碼。
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();
}
}
這裏MPAGE是動態創建的佈局視圖。下面的 是我的適配器代碼。
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;
}
}
請有人幫我解決這個問題。
請問我可以知道投票的原因嗎 – Raj
您能否顯示版面和代碼 –