2016-09-23 8 views
-3

내가 안드로이드에서 응용 프로그램을하고 있어요 내가 몇 조각을 설정하기 위해 노력하고있어하지만 응용 프로그램을 실행할 때마다이 두 가지 오류와 충돌 :안드로이드 조각 오류 팽창 클래스 단편

MainActivity: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class fragment

[email protected] must implement OnFragmentInteractionListener

인터넷 검색 많은 사람들이 XML 파일을 android :: name 클래스에서 변경했다고 생각했습니다. 나는 그것을했고 아직도 나에게 일어난다. 내 조각의 XML

public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     Fragment fragment = new Fragment(); 

     if (id == R.id.nav_camera) { 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.replace(R.id.search_fragment, fragment); 
      ft.commit(); 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

:

<FrameLayout 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" 
    tools:context=".SearchFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

그리고 나는 activity_main에 구현 :

<fragment android:id="@+id/search_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.onelio.app.SearchFragment"/> 

어떻게 할 수

나는 조각으로 이동이 코드를 사용 고쳐? 감사합니다.

답변

0

정적 및 동적 인 Fragment을 혼용하려고합니다. 레이아웃에 <fragment> 요소를 포함 시키면 시스템에 클래스 com.onelio.app.SearchFragment을 부 풀어서 레이아웃에 삽입하라는 메시지가 표시됩니다. 동적으로 변경할 수 없습니다.

당신은이 Activity 소유 내에 FragmentTransaction(A)에 통해 첨가 될 수 동적 단편을 만드는 (아직 새로운 SearchFragment() 또는 더 호출 정적 메소드 SearchFragment.createInstance() 통해) 새로운 단편을 만들 때 (또는 기타 Fragment) 레이아웃.

Fragment 사용에 대한 자세한 내용과 예제 코드는 this page을 참조하십시오.

관련 문제