2014-12-31 82 views
-1

如何在連接fragmentssUnreachabe代碼,而抽屜式導航

private void launchFragment(int paramInt) 
      { 
      String str="Photo"; 
      Object localObject=new Photo(); 
      if (paramInt == 0) 
      { 
       str = "Photo"; 
       localObject = new Photo(); 
       if (localObject == null); 
      } 
      while(true) 
      { 
       if (getSupportFragmentManager().getBackStackEntryCount() <= 0) 
       { 
       getSupportFragmentManager().beginTransaction().replace(R.id.drawer_layout, (Fragment)localObject).addToBackStack(str).commit(); 
       return; 
    //from this line its inidicates unreachable code    
    if (paramInt == 1) 
       { 
        str = "Activity"; 
        localObject = new ActivityList(); 
        break; 
       } 
       if (paramInt == 2) 
       { 
        str = "Explore Video"; 
        localObject = new Explore(); 
        break; 
       } 
       if (paramInt == 3) 
       { 
        str = "Profile"; 
        localObject = new Profile(); 
        break; 
       } 
       if (paramInt == 4) 
       { 
        str = "Upload new file"; 
        localObject = new Upload(); 
        break; 
       } 
       localObject = null; 
       str = null; 
       if (paramInt != 5) 
        break; 
       str = "Elements"; 
       localObject = new Elements(); 
       break; 
       } 
       getSupportFragmentManager().popBackStackImmediate(); 
      } 
      } 
+1

你的代碼是在語句'return;'之後。當然,它無法達到。在return之前移動所有的代碼 –

回答

0

return語句必須是該方法的最後一條語句解決這個可達代碼。一個方法在return聲明處停止執行。因此,重新考慮您的代碼,以便在返回語句之前,您可以做任何你想要的東西。然後放置你的條件(或其他),並返回你想要返回的任何東西。

0

代碼無法訪問,因爲之前有一個return語句,因此無法執行。試試這個:

private void launchFragment(int paramInt) 
     { 
     String str="Photo"; 
     Object localObject=new Photo(); 
     if (paramInt == 0) 
     { 
      str = "Photo"; 
      localObject = new Photo(); 
      if (localObject == null); 
     } 
     while(true) 
     { 
      if (getSupportFragmentManager().getBackStackEntryCount() <= 0) 
      { 
      getSupportFragmentManager().beginTransaction().replace(R.id.drawer_layout, (Fragment)localObject).addToBackStack(str).commit(); 

//from this line its inidicates unreachable code    
if (paramInt == 1) 
      { 
       str = "Activity"; 
       localObject = new ActivityList(); 
       break; 
      } 
      if (paramInt == 2) 
      { 
       str = "Explore Video"; 
       localObject = new Explore(); 
       break; 
      } 
      if (paramInt == 3) 
      { 
       str = "Profile"; 
       localObject = new Profile(); 
       break; 
      } 
      if (paramInt == 4) 
      { 
       str = "Upload new file"; 
       localObject = new Upload(); 
       break; 
      } 
      localObject = null; 
      str = null; 
      if (paramInt != 5) 
       break; 
      str = "Elements"; 
      localObject = new Elements(); 
      break; 
      } 
      getSupportFragmentManager().popBackStackImmediate(); 
     } 
     return; 
    }