2016-08-16 6 views
-1

私はここナビゲーションドロワー最新のコード

ベースの活動にナビゲーション引き出しを作成し、主な活動に延びているが、何の結果が主な活動ではありません

私のコードは次のとおりです。

BaseActivity Javaコード:

public class BaseActivity extends AppCompatActivity { 
     DrawerLayout mDrawerLayout; 
     ListView mDrawerList; 
     String[] items; 
     ActionBarDrawerToggle mDrawerToggle; 
     Toolbar toolbar; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
      mDrawerList = (ListView) findViewById(R.id.drawer_list); 
      toolbar = (Toolbar) findViewById(R.id.tool_bar); 
      setSupportActionBar(toolbar); 
      items = getResources().getStringArray(R.array.menu); 
      mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, items)); 
      mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){ 

      }; 
      mDrawerLayout.addDrawerListener(mDrawerToggle); 
      mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        selectItem(position); 
        mDrawerLayout.closeDrawer(mDrawerList); 

       } 
      }); 

     } 
     public void selectItem(int position){ 
      Intent intent; 
      switch (position) 
      { 
       case 0: 
        intent = new Intent(this,OneActivity.class); 
        startActivity(intent); 
        break; 
       case 1: 
        intent = new Intent(this,TwoActivity.class); 
        startActivity(intent); 
        break; 
      } 
     } 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

      if (mDrawerToggle.onOptionsItemSelected(item)) { 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 

     } 
     @Override 
     protected void onPostCreate(Bundle savedInstanceState) { 
      super.onPostCreate(savedInstanceState); 
      mDrawerToggle.syncState(); 
     } 
     @Override 
     public void onConfigurationChanged(Configuration newConfig) { 
      super.onConfigurationChanged(newConfig); 
      mDrawerToggle.onConfigurationChanged(newConfig); 
     } 

    } 

そして、ここに私のXMLコードです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:toolbar="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/tool_bar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorAccent" 
      toolbar:navigationIcon="@drawable/ic_navigation"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.v4.widget.DrawerLayout 
      android:id="@+id/drawer_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

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

      <ListView 
       android:id="@+id/drawer_list" 
       android:layout_width="240dp" 
       android:layout_height="match_parent" 
       android:layout_gravity="start" 
       android:background="@color/colorAccent" 
       android:choiceMode="singleChoice" 
       android:divider="@android:color/holo_blue_dark" 
       android:dividerHeight="1dp"> 

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

そしてここMainActivityです:

public class MainActivity extends BaseActivity { 

     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(layout.activity_main); 

        } 

     } 

は私に正しい答えを示唆して

答えて

0

baseActivityクラスのコンストラクタを作成し、コンストラクタで、あなたMainActivityにそのコンストラクタを呼び出すよりも、baseActivityクラスのあなたの定義された関数を呼び出しますクラス関数onCreate(Bundle savedInstanceState)

関連する問題