导航抽屉中无法访问的代码

时间:2014-12-31 12:57:28

标签: android

如何在连接fragmentss

时解决此无法访问的代码
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();
            }
          }

2 个答案:

答案 0 :(得分:0)

return语句必须是方法的最后一个语句。方法在return语句处停止执行。因此,重新考虑代码,以便在返回语句之前执行所需的一切。然后放置你的if条件(或其他)并返回你想要返回的任何内容。

答案 1 :(得分: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;
    }