2014-06-11 8 views
0

현재 탭과 OptionsMenu가있는 ActionBar가 있습니다. 내 탭은 각 프래그먼트를 제대로 열지 만 onOptionsItemSelected를 사용하여 프래그먼트를 여는 방법을 알 수 없습니다.Android onOptionsItemSelected 새 단편을 엽니 다.

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_info) { 
      new ParentInfoSectionFragement(); 
      return true; 
     } else if (id == R.id.action_history){ 
      new NotificationHistorySectionFragment(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

내가 시도한 것이 분명 잘못되었습니다.

모든 도움을 주시면 감사하겠습니다.

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.parentprojectmobile.MainActivity" /> 

fragment_history.xml

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.parentprojectmobile.MainActivity$PlaceholderFragment" > 

    <TextView 
     android:id="@+id/history_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/text_history" /> 

/RelativeLayout> 

모든 조각이 원래 조각의 단지 복사본입니다 만 목적을 학습 textviews을 주셔서 감사합니다.

답변

0

사용 활동의 조각 거래

예 : R.id.layout_id 당신이 당신의 조각을 배치 할 레이아웃 ID입니다

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
    transaction.replace(R.id.layout_id, new ParentInfoSectionFragement(), null).commit(); 

.

+0

답장을 보내 주셔서 감사합니다. 불행히도 메뉴 항목을 선택하면 불행히도 앱이 중지되었습니다. message – Photekk

+0

@Photekk이 게시물에 오류를 게시합니다. –

+0

내 전화가 튕겨 나오는 팝업은 내가받은 – Photekk

0

는이 예제 R.id.fragment_container에서 당신의 조각 스택

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_info) { 
     new ParentInfoSectionFragement(); 
     return true; 
    } else if (id == R.id.action_history){ 
     new NotificationHistorySectionFragment(); 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     notificationHistorySectionFragment = new NotificationHistorySectionFragment(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, notificationHistorySectionFragment); 
     fragmentTransaction.addToBackStack(null); 

     //Commit Transaction 
     fragmentTransaction.commit(); 
    } 
    return super.onOptionsItemSelected(item); 
} 

을 처리하기 위해 필요한 것은 레이아웃에 조각 컨테이너의 ID입니다.

+0

"형식 불일치 : android.support.v4.app.FragmentManager에서 android.app.FragmentManager로 변환 할 수 없습니다."FragmentManager fragmentManager = getFragmentManager();에서 가져 오는 오류입니다. – Photekk

+0

아, 글쎄요. 당신이 지원 조각 관리자를 사용했다, 나는 코드를 업데이 트합니다, 아주 간단한 변화. 내가 변경 한 사항은'getFragmentManager()'에서'getSupportFragmentManager()'로 변경하는 것입니다. –

+0

진술에 대해 사과드립니다. 아직도 이것에 아주 새롭고 깨닫지 않았다. 나는 필요한 변화를했다. 이제 옵션 메뉴에서 항목을 선택하면 "불행히도 이 중지되었습니다."라는 메시지가 나타납니다. – Photekk

관련 문제