2015-01-14 2 views
0

다음 코드를 사용하여 Android 개발자 사이트에 제공된 샘플을 기반으로 간단한 탐색 창을 구현합니다.Android 탐색 서랍 콘텐츠 프레임을 해결할 수 없습니다.

public class MainActivity extends ActionBarActivity { 

    Toolbar toolbar; 
    DrawerLayout mDrawerLayout; 
    private CharSequence mDrawerTitle; 
    private CharSequence mTitle; 
    ListView mDrawerList; 
    private String[] mDrawerItems; 
    ActionBarDrawerToggle drawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     mDrawerItems = getResources().getStringArray(R.array.graph_array); 
     mDrawerList = (ListView) findViewById(R.id.list_view_drawer); 
     setSupportActionBar(toolbar); 
     mTitle = mDrawerTitle = getTitle(); 
     toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.navDrawer); 
     mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark)); 
     drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.abc_action_bar_home_description, R.string.abc_action_bar_home_description) { 

      public void onDrawerClosed(View view) { 
       getSupportActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 


     }; 

     mDrawerLayout.setDrawerListener(drawerToggle); 

     if (savedInstanceState == null) { 
      selectItem(0); 
     } 

     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mDrawerItems)); 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
    } 

    private class DrawerItemClickListener implements ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      selectItem(position); 
     } 
    } 

    private void selectItem(int position) { 
     // update the main content by replacing fragments 
     //Fragment fragment = new PlanetFragment(); 
     //Bundle args = new Bundle(); 
     //args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); 
     //fragment.setArguments(args); 

     if (position == 0) { 
      Fragment frag1 = new Fragment1(); 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction().replace(R.id.content_frame, frag1).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      setTitle(mDrawerItems[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else if (position == 1) { 

     } 
    } 



     @Override 
    public void setTitle(CharSequence title) { 
     mTitle = title; 
     getSupportActionBar().setTitle(mTitle); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     // Pass any configuration change to the drawer toggls 
     drawerToggle.onConfigurationChanged(newConfig); 
    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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); 

    } 

    public static class PlanetFragment extends Fragment { 
     public static final String ARG_PLANET_NUMBER = "planet_number"; 

     public PlanetFragment() { 
      // Empty constructor required for fragment subclasses 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_planet, container, false); 
      int i = getArguments().getInt(ARG_PLANET_NUMBER); 
      String planet = getResources().getStringArray(R.array.graph_array)[i]; 

      int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), 
        "drawable", getActivity().getPackageName()); 
      ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId); 
      getActivity().setTitle(planet); 
      return rootView; 
     } 
    } 
} 

는하지만 "content_frame가 해결 될 수 없습니다"라는 오류가 발생합니다. 이 문제를 어떻게 해결할 수 있습니까?

편집 :

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/navDrawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar" /> 

    </LinearLayout> 

    <com.example.shivam.signalprocessing1.ScrimInsetsFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/scrimInsetsFrameLayout" 
     android:layout_width="320dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@color/white" 
     android:elevation="10dp" 
     android:fitsSystemWindows="true" 
     app:insetForeground="#4000"> 


     <RelativeLayout 
      android:id="@+id/left_drawer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="@color/white" 
      android:fitsSystemWindows="true" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/image_view" 
       android:layout_width="match_parent" 
       android:layout_height="144dp" 
       android:scaleType="centerCrop" 
       android:background="@drawable/material_wallpaper" /> 

      <ListView 
       android:id="@+id/list_view_drawer" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/image_view" 
       android:choiceMode="singleChoice" /> 
     </RelativeLayout> 

    </com.example.shivam.signalprocessing1.ScrimInsetsFrameLayout> 


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

감사합니다!

+0

xml에'content_frame' id가있는 레이아웃이 있습니까? –

+0

XML 파일 (activity_main.xml)을 게시하십시오. – bonnyz

+0

업데이트 된 질문을 확인하십시오. 프레임 레이아웃을 사용하지 않았기 때문에 오류가 발생합니다. 그런데 JAVA 코드에서 그 대신에 무엇을 사용합니까? –

답변

-1

프레임의 ID를 변경하십시오 ... 일반적으로 컨테이너 ... 그러나 content_frame으로 변경하면 좋습니다.