【发布时间】:2013-12-23 23:29:18
【问题描述】:
我是开发Android的新手,现在在Fragment之间调用方法时遇到问题。让我描述一下,希望大家能帮我解决。
片段 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();
}
}
这是我的类,我想在片段A中执行片段B中的方法“showContent”,但我只能显示Alert,但方法“showContent”总是出现崩溃错误。
请告诉我你对这个案子的想法。 非常感谢。
【问题讨论】:
-
添加你的 logcat 输出。