2

안드로이드 스튜디오의 기본 탐색 창 활동에서 조각을 부풀리기 위해 this 자습서를 따르고 있습니다. 컴파일 후 난 logcat에 다음과 같은 오류가 발생 나는 조각을 부 풀릴 수 없습니다.칸트가 동적으로 조각을 펼칩니다.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mayurn.sidepanel2/com.example.mayurn.sidepanel2.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #16: Error inflating class fragment 

MainActivity.java

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

public static String TAG = "mytag"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);   //set Action bar 
    setSupportActionBar(toolbar); 


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  //Fab snackbar 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);   //Drawer parameters 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState();           //show present view in drawer 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    displaySelectedFrame(R.id.nav_camera); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) {         //inflate the setting menu 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 

    displaySelectedFrame(item.getItemId()); 

    return true; 

} 

public void displaySelectedFrame(int itemId){ 

    Fragment f = null; 

    switch(itemId){ 

     case R.id.nav_camera: 
      f = new frag_1(); 
      break; 

     case R.id.nav_gallery: 
      f = new frag_2(); 
      break; 

     case R.id.nav_manage: 
      f = new frag_3(); 
      break; 
    } 

    if(f!=null){ 

     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.replace(R.id.content_fragment , f); 
     ft.commit(); 

    } 

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

}

이제 중요한 부분은 - >>

내 레이아웃과 관련이 여기 activity_main->(include)->app_bar_main->(include)->content_main->(contain)fragment

있는 파일과 같다.

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

app_bar_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.mayurn.sidepanel2.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

content_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.mayurn.sidepanel2.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/content_fragment" 
     /> 

</RelativeLayout> 

지금 내가 조각을 부풀려 질수 원인이 조각 태그라고 생각합니다. 미리보기 레이아웃도 렌더링 오류가 있기 때문에 나는 android:name 매개 변수를주지 않았다.

하지만 사용자가 탐색 패널에서 항목을 클릭하면 아무 것도 사용하지 않고로드하려고합니다.

이 줄을 추가하면 android:name="com.example.mayurn.sidepanel2.frag_1" 응용 프로그램이 실행되지만 frag_1과 같이 겹치는 부분이 뚜렷하게 나타나고 다른 부분이 겹칩니다.

android:name 매개 변수없이 동적으로 조각을로드하려고합니다.

도움을 주셔서 감사합니다.

+4

''을 (를) ''(으)로 변경하십시오. 'Fragment'를 동적으로로드하고 있기 때문에'View'를 유지하기 위해서는 빈'ViewGroup' 만 있으면됩니다. ''태그는 정적'Fragment'를위한 것입니다. –

+0

@MikeM. 대답해라. –

+1

멋지다. 원할 경우 게시 된 답변을 수락 할 수 있습니다. 나는 복제물을 찾고 있었지만, 지금은보기 흉하지 않은 것 같습니다. –

답변

2

조각 태그 대신 사용하십시오.

<FrameLayout 
     android:id="@+id/frame_parent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
+0

감사합니다.하지만 @ mikeM에 의해 해결되었습니다.이 사람이 대답하거나 받아 들일 때까지 기다려야합니다. 혼란 스럽습니다. –

관련 문제