我是開發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」總是得到崩潰的錯誤。
請給我你這個案件的想法。 非常感謝。
添加您的logcat輸出。 – Booger