2017-11-11 148 views
2

我一直在製作具有導航抽屜的應用程序,但該導航抽屜與我製作的開關盒不兼容。編號是正確的。Android Studio Swictch案例實例無法正常工作

對於初學者來說這裏是我的代碼的簡化版本:

這是我的java類:

public class Image extends AppCompatActivity { 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mToggle; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_image); 

     //DRAWER LAYOUT 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close); 
     mDrawerLayout.addDrawerListener(mToggle); 
     mToggle.syncState(); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     NavigationView mNavigationView = (NavigationView) findViewById(R.id.nav_menu); 
     mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){ 
      @Override public boolean onNavigationItemSelected(MenuItem menuItem) 
      { switch (menuItem.getItemId()) 
      { 
       case(R.id.nav_account): Intent accountActivity = new Intent(getApplicationContext(), Welcome.class); 
        startActivity(accountActivity); 
       case(R.id.nav_exercises): Intent accountActivity1 = new Intent(getApplicationContext(), Video.class); 
        startActivity(accountActivity1); 
       case(R.id.nav_tips): Intent accountActivity2 = new Intent(getApplicationContext(), Image.class); 
        startActivity(accountActivity2); 
      } 
       return true; 
      }); 

     //Navigation Drawer 



} 



    //FOR NAVIGATION DRAWER 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 


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

    } 
    //Navigation Drawer End 


} 

這裏是XML的:

navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@+id/nav_account" 
     android:icon="@mipmap/ic_person_outline_black_24dp" 
     android:title="My Account" /> 

    <item android:id="@+id/nav_settings" 
     android:icon="@mipmap/ic_settings_black_24dp" 
     android:title="Settings"/> 

    <item android:id="@+id/nav_exercises" 
     android:icon="@mipmap/ic_accessibility_black_24dp" 
     android:title="Exercises"/> 

    <item android:id="@+id/nav_tips" 
     android:icon="@mipmap/ic_face_black_24dp" 
     android:title="Tips"/> 

    <item android:id="@+id/nav_scheduler" 
     android:icon="@mipmap/ic_date_range_black_24dp" 
     android:title="My Schedule"/> 

    <item android:id="@+id/nav_info" 
     android:icon="@mipmap/ic_info_outline_black_24dp" 
     android:title="Info"/> 

    <item android:id="@+id/nav_logout" 
     android:icon="@mipmap/ic_input_black_24dp" 
     android:title="Logout"/> 

</menu> 

這裏就是我申請的抽屜:

<?xml version="1.0" encoding="utf-8"?> 


<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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.Welcome.Video" 
    android:id="@+id/drawerLayout"> 

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

    <VideoView 
     android:id="@+id/videoView" 
     android:layout_width="wrap_content" 
     android:layout_height="259dp" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="videoplay" 
     android:text="Play" /> 
</LinearLayout> 


<android.support.design.widget.NavigationView 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
app:menu="@menu/navigation_menu" 
app:headerLayout="@layout/navigation_header" 
android:layout_gravity="start" 
android:id="@+id/nav_menu" 
    > 


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


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

當導航抽屜裏,我點擊nav_exercises情況,但顯示的內容是的應該是爲nav_tips的觀點發生了什麼。我點擊回到練習部分。 TIPS正在運行。

但是練習部分,它顯示TIPS視圖,然後當你按下「返回」它會去正確的地方。換句話說,它與其他觀點有某種重疊。

我做錯了什麼?我確定班級和身份證是在正確的地方,我已經檢查了一個小時。

任何幫助表示讚賞。非常感謝你!

+0

應該始終參考[官方教程]( https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)和其他在線文檔,然後再搜索/詢問其他地方。 –

回答

2

需要,否則添加break;任何情況下都會被執行

switch (menuItem.getItemId()) 
{ 
    case(R.id.nav_account): Intent accountActivity = new Intent(getApplicationContext(), Welcome.class); 
     startActivity(accountActivity); 
     break; 
     //^^^ 
    case(R.id.nav_exercises): Intent accountActivity1 = new Intent(getApplicationContext(), Video.class); 
     startActivity(accountActivity1); 
     break; 
     //^^^ 
    case(R.id.nav_tips): Intent accountActivity2 = new Intent(getApplicationContext(), Image.class); 
     startActivity(accountActivity2); 
     break;// not needed at last but good practice 
} 
0

你需要打破開關罩

switch (request.getMethod()) { 
      case Method.GET: 
       ... 
       break; 
      case Method.DELETE: 
       ...  
       break; 
      case Method.POST: 
       ... 
       break; 
      case Method.PUT: 
       ... 
       break; 
      default: 
       throw new IllegalStateException("Unknown method type."); 
     } 
相關問題