2015-12-08 117 views
1

我正在開發一個自助服務終端應用,人們可以步行去訂購東西。通過活動訪問片段方法

我有一堆不同的片段(訂單,審查,支付等)的單一活動。一些互相替換,一些被添加。該活動有一個硬編碼的片段R.id.fragmentContainer,其餘的以編程方式添加和標記。

現在我想要的是在我的活動中有一個函數調用片段的元素之一(請看updateReceivedData()),但由於某種原因我無法這樣做,並且我得到cannot resolve method adjustPriceFunc

我添加了代碼,所以有人可以告訴我我做錯了什麼。該應用程序通常會與某些硬件進行通信,因此我從代碼中提取了一些細節以便於閱讀。

public class MainActivity extends FragmentActivity { 
private final String TAG = MainActivity.class.getSimpleName(); 

List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Log.d(TAG, "onCreate()"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.backbone); 
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 
    android.support.v4.app.Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); 

    if (fragment == null) { 
     fragment = new Fragment(); 
     fm.beginTransaction() 
       .add(R.id.fragmentContainer, fragment, "SystemDownFragment") 
       .addToBackStack(null) 
       .commit(); 
    } 
} 

public void orderFunc() { 
    Log.d(TAG, "orderFunc()"); 
    if(sendingLogFlag)  attemptSend(TAG + ":orderFunc()"); 

    for(int i = 0; i < getActiveFragments().size(); i++) { 
     getSupportFragmentManager().beginTransaction().remove(getActiveFragments().get(i)).commit(); 
    } 

    Fragment newFragment = new Fragment(); 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.fragmentContainer, newFragment, "OrderFragment"); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.commit(); 
} 

public void checkoutFunc() { 
    Log.d(TAG, "checkoutFunc()"); 
    if(sendingLogFlag)  attemptSend(TAG + ":checkoutFunc()"); 

    for(int i = 0; i < getActiveFragments().size(); i++) { 
     getSupportFragmentManager().beginTransaction().remove(getActiveFragments().get(i)).commit(); 
    } 

    Fragment newFragment = new Fragment(); 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.fragmentContainer, newFragment, "CheckOutFragment"); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.commit(); 
} 

public void reviewFunc(){ 
    Log.d(TAG, "reviewFunc()"); 
    if(sendingLogFlag)  attemptSend(TAG + ":reviewFunc()"); 

    if(getActiveFragments().size() > 1){ 
     Fragment frag = getSupportFragmentManager().findFragmentByTag("ReviewFragment"); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); 
     ft.remove(frag); 
     ft.commit(); 
    } 
    else { 
     Fragment newFragment = new Fragment(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     ft.add(R.id.fragmentContainer, newFragment, "ReviewFragment"); 
     ft.commit(); 
    } 
    getSupportFragmentManager().executePendingTransactions(); 
} 

public void payFunc(boolean enabled){ 
    Log.d(TAG, "payFunc()"); 
    if(sendingLogFlag)  attemptSend(TAG + ":payFunc()"); 

    if(!enabled){ 
     Fragment frag = getSupportFragmentManager().findFragmentByTag("PayFragment"); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); 
     ft.remove(frag); 
     ft.commit(); 
    } 
    else { 
     Fragment newFragment = new Fragment(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     ft.add(R.id.fragmentContainer, newFragment, "PayFragment"); 
     ft.commit(); 
    } 
    getSupportFragmentManager().executePendingTransactions(); 
} 

public void doneFunc() { 
    Log.d(TAG, "doneFunc()"); 

    for(int i = 0; i < getActiveFragments().size(); i++) { 
     getSupportFragmentManager().beginTransaction().remove(getActiveFragments().get(i)).commit(); 
    } 

    Fragment newFragment = new Fragment(); 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.add(R.id.fragmentContainer, newFragment, "StartupFragment"); 
    ft.commit(); 
} 

@Override 
public void onAttachFragment (Fragment fragment) { 
    fragList.add(new WeakReference(fragment)); 
} 

public List<Fragment> getActiveFragments() { 
    ArrayList<Fragment> ret = new ArrayList<Fragment>(); 
    for(WeakReference<Fragment> ref : fragList) { 
     Fragment f = ref.get(); 
     if(f != null) { 
      if(f.isVisible()) { 
       ret.add(f); 
      } 
     } 
    } 
    return ret; 
} 

private void updateReceivedData() { 
    Fragment frag = getSupportFragmentManager().findFragmentByTag("PayFragment"); 
         frag.adjustPriceFunc(); 
} 

回答

2

您正在嘗試使用方法adjustPriceFunc()Fragment類,這是不是它的一部分。

使用此代碼,其中MyFragment是你的正確片段(我認爲它應該是PayFragment)。

private void updateReceivedData() { 
    Fragment frag = getSupportFragmentManager().findFragmentByTag("PayFragment"); 
    if (frag instanceof MyFragment) { 
     frag.adjustPriceFunc(); 
    }     
} 
+0

這並獲得成功。謝謝 –

0

即使它是WeakReference,存儲對片段的引用也是一種不好的做法。
更好地利用這種方式:

Fragment fragment = getSupportFragmentManager().findFragmentByTag("tag"); 
2

這裏有一些錯誤代碼

  • orderFunccheckoutFunc等添加remove的交易,並在循環後調用commit一次,像

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    for(int i = 0; i < getActiveFragments().size(); i++) { 
        transaction.remove(getActiveFragments().get(i)); 
    } 
    transaction.commit(); 
    
  • 在中得到編譯錯誤,因爲Fragment沒有adjustPriceFunc()方法。您需要用(清潔方法)創建接口adjustPriceFunc()方法,讓你的片段執行它,然後你updateReceivedData看起來像如下:

    private void updateReceivedData() { 
        InterfaceWithAdjustPriceMethod frag = (InterfaceWithAdjustPriceMethod) getSupportFragmentManager().findFragmentByTag("PayFragment"); 
        frag.adjustPriceFunc(); 
    } 
    
+0

呵呵,我完全沒有抓住前面的錯誤。感謝您指出 –