2013-12-23 45 views
0

我是開發Android的新手,現在我在調用片段之間的方法時遇到問題。讓我描述一下,我希望每個人都能幫我解決它。如何從片段執行片段B中的方法A

片段A

public class A extends Fragment implements OnItemClickListener { 
    ......... 
    ......... 
    ......... 
    public void showContent(int pSelectedIndex, int pSelectedSubIndex) { 
    // Create fragment and give it an argument specifying the article it 
    RelativeLayout thisTopLayout = (RelativeLayout)getActivity().findViewById(R.id.directoryTopRelativeLayout); 
    thisTopLayout.setVisibility(LinearLayout.GONE); 
    RelativeLayout thisBodyLayout = (RelativeLayout)getActivity().findViewById(R.id.directoryBodyRelativeLayout); 
    thisBodyLayout.setVisibility(LinearLayout.GONE); 

    FragmentTransaction transaction = getFragmentManager() 
      .beginTransaction(); 
    if (pSelectedIndex == 1) { 
     BusinessView thisItem = new BusinessView(); 
     transaction.replace(R.id.directoryLayout, thisItem); 
     thisItem.DetectContentType(pSelectedSubIndex, this.getActivity()); 
    } 
     } 
} 

片段B

public class B extends Fragment implements OnItemClickListener { 
    @SuppressWarnings("deprecation") 
public void DetectContentType(int selectedType, Activity pActivity){ 
    if (selectedType != 1) { 
     AlertDialog alertDialog = new AlertDialog.Builder(pActivity) 
       .create(); 
     alertDialog.setTitle("EXAMPLE"); 
     alertDialog 
       .setMessage("SHOW MESSAGE"); 
     alertDialog.setButton("YES", new DialogInterface.OnClickListener() { 
      public void onClick(final DialogInterface dialog, 
        final int which) { 
       // here you can add functions 
      } 
     }); 
     alertDialog.setButton2("NO", new DialogInterface.OnClickListener() { 
      public void onClick(final DialogInterface dialog, 
        final int which) { 
       // here you can add functions 
      } 
     }); 
     alertDialog.show(); 
    } else { 
     showContent(selectedType); 
    } 
} 

public void showContent(int pSelectedIndex) { 
    // Create fragment and give it an argument specifying the article it 
    RelativeLayout thisTopLayout = (RelativeLayout) this.getActivity().findViewById(R.id.businessTopRelativeLayout); 
    thisTopLayout.setVisibility(LinearLayout.GONE); 
    RelativeLayout thisBodyLayout = (RelativeLayout) this.getActivity() 
      .findViewById(R.id.businessBodyRelativeLayout); 
    thisBodyLayout.setVisibility(LinearLayout.GONE); 

    FragmentTransaction transaction = getFragmentManager() 
      .beginTransaction(); 
    BusinessItemView thisItem = new BusinessItemView(pSelectedIndex); 
    transaction.replace(R.id.businessLayout, thisItem); 
    // Commit the transaction 
    transaction.commit(); 
} 
} 

這是我的課,我想在B片段執行法 「showContent」 當我在片段A,但我可以只顯示警報,但方法「showContent」總是得到崩潰的錯誤。

請給我你這個案件的想法。 非常感謝。

+0

添加您的logcat輸出。 – Booger

回答

2

在片段A中,您希望稍微改變方法調用;直到片段已經通過交易連接,通過調用commit在B片段

FragmentTransaction transaction = getFragmentManager() 
     .beginTransaction(); 
if (pSelectedIndex == 1) { 
    BusinessView thisItem = new BusinessView(); 
    transaction.replace(R.id.directoryLayout, thisItem); 
    transaction.commit(); 
    thisItem.DetectContentType(pSelectedSubIndex, this.getActivity()); 
} 

你DetectContentType方法不能做任何事情AlertDialogs。

同樣在您的具體情況下,儘管提供了用於AlertDialog構建器的活動,對於您的情況應該沒問題,但如果您提交的事務是非必需的,則您通過pSelectedSubIndex傳遞,但不知道來自您的例如,您可能在片段B上碰到showContent(),這肯定會失敗,因爲直到您啓動片段生命週期時才能附加視圖。

+0

感謝您的回答馬庫斯。這對我來說是非常有幫助的。 但不幸的是,即使我設置「提交」來完成該事務,我仍然無法運行。 –

相關問題