2013-12-23 4 views
0

저는 안드로이드 개발에 초보자입니다. 이제 프래그먼트간에 메소드를 호출 할 때 문제가 발생합니다. 설명해 드리겠습니다. 모두가 해결할 수 있도록 노력하겠습니다.프래그먼트 A에서 프래그먼트 B의 메소드를 실행하는 방법

조각

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"를 실행하고자한다

,하지만 난 그냥 표시 할 수 있습니다 경고하지만 "showContent"메서드는 항상 오류가 발생합니다.

이 사례에 대해 알려주십시오. 정말 고마워요.

+0

logcat 출력을 추가하십시오. – Booger

답변

2

단편 A에서는 약간의 메소드 호출을 변경하고자합니다. 조각이 커밋 호출하여 트랜잭션을 통해 부착 될 때까지 조각 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

답변 해 주신 Marcus에게 감사드립니다. 그것은 나를 위해 매우 도움이됩니다. 하지만 불행히도, 나는 그 거래를 끝내기 위해 "커밋 (commit)"을 설정했다고해도 여전히 실행할 수 없습니다. –

관련 문제