我正在開發Android應用程序的平板電腦,而不是使用兼容性庫。IllegalArgumentException:在快速切換時未找到針對片段ID的視圖ActionBar Tabs
只有一個活動,它使用帶有3個選項卡的ActionBar。 在TabListener中,我使用setContentView加載特定於該選項卡的佈局,然後將相關片段添加到其框架中。 這個差不多的工作方式和我想要的完全一樣,除非在標籤之間快速切換時,應用程序會崩潰。
我正在使用三星Galaxy Tab作爲我的調試設備,切換標籤非常快。以正常速度,我可以在它們之間來回切換,並且頁面立即加載。問題是當我在標籤之間超級切換時。
起初,我得到了一個
IllegalStateException: Fragment not added
如下所示: http://code.google.com/p/android/issues/detail?id=17029 繼建議使用try/catch塊中onTabUnselected,我所做的應用程序多了幾分穩健,但導致的問題手頭:
IllegalArgumentException: No view found for id 0x... for fragment ...
我還沒有發現任何人有同樣的問題在網絡上的任何其他情況下,所以我很擔心,我會那樣做,這不是支持的東西。 再次,我想要做的是在一個Activity中使用3種不同的佈局 - 當您單擊一個選項卡時,監聽器將調用setContentView來更改佈局,然後添加片段。除非您開始積極地在標籤之間切換,否則它的效果非常好。
我的想法是:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html 而不是TabListener保留一個片段的引用,我有他們的數組。另外,我沒有使用attach/detach,因爲這些只是在API 13中添加的。
我的理論是setContentView還沒有完成創建視圖,這就是爲什麼FragmentTransaction無法添加它們,或者這些片段是當另一個選項卡被選中並且setContentView被調用時,被添加到一個選項卡,銷燬另一組視圖。
我試圖破解某些內容以減慢標籤切換,但沒有得到任何地方。
這裏是代碼爲我TabListener:
private class BTabListener<T extends Fragment> implements ActionBar.TabListener{
private int mLayout;
private Fragment[] mFrags;
private TabData mTabData;
private Activity mAct;
private boolean mNoNewFrags;
public BTabListener(Activity act, int layout, TabData td, boolean frags){
mLayout = layout;
mTabData = td;
mAct = act;
mNoNewFrags = frags;
mFrags = new Fragment[mTabData.fragTags.length];
for(int i=0; i<mFrags.length; i++){
//on an orientation change, this will find the fragments that were recreated by the system
mFrags[i] = mAct.getFragmentManager().findFragmentByTag(mTabData.fragTags[i]);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//this gets called _after_ unselected
//note: unselected wont have been called after an orientation change!
//we also need to watch out because tab 0 always gets selected when adding the tabs
//set the view for this tab
mAct.setContentView(mLayout);
for(int i=0; i<mFrags.length; i++){
//this will be null when the tab is first selected
if(mFrags[i]==null){
mFrags[i] = Fragment.instantiate(GUITablet.this, mTabData.classes[i].getName());
}
//if there was an orientation change when we were on this page, the fragment is already added
if(!mNoNewFrags || mDefaultTab!=tab.getPosition()){
ft.add(mTabData.containterIDs[i], mFrags[i], mTabData.fragTags[i]);
}
}
mNoNewFrags = false;
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// this gets called when another tab is selected, before it's onSelected method
for(Fragment f : mFrags){
try{ //extra safety measure
ft.remove(f);
}catch(Exception e){
e.printStackTrace();
System.out.println("unselect couldnt remove");
}
}
}
}
最後,堆棧跟蹤:
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): java.lang.IllegalArgumentException: No view found for id 0x7f0b0078 for fragment Fraggle{40ab2230 #2 id=0x7f0b0078 dummy2}
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:729)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.app.BackStackRecord.run(BackStackRecord.java:578)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1226)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:374)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.os.Handler.handleCallback(Handler.java:587)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.os.Looper.loop(Looper.java:132)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at android.app.ActivityThread.main(ActivityThread.java:4028)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at java.lang.reflect.Method.invoke(Method.java:491)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
09-29 01:53:08.200: ERROR/AndroidRuntime(4611): at dalvik.system.NativeStart.main(Native Method)
謝謝!!
任何一個可以幫助我擺脫這種錯誤的.... [鏈接] [1] [1]:http://stackoverflow.com/questions/2540 0641/fragmentmanager-illegalargumentexception-no-view-found-for-id-for-fragment?noredirect = 1 –
@SamKazmi,你是否嘗試過建議的答案......? – hooby3dfx