0

我一整天都在编程,现在我在返回后无法访问;在

return;
Recipe093.path[1] = localCursor.getString(1);

如果我删除返回,我会在继续后得到无法访问的代码;为什么我得到无法访问的代码?希望有人可以帮助我,谢谢。这是我的代码:

public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.musiclist);
    final Cursor localCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, (String[])null, null, (String[])null, null);
    String[] arrayOfString1 = localCursor.getColumnNames();
    int i = arrayOfString1.length;
    for (int j = 0; ; j++)
    {
      if (j >= i)
      {
        String[] arrayOfString2 = { "title", "artist", "duration" };
        int[] arrayOfInt = { 2131099668, 2131099669, 2131099670 };
        SimpleCursorAdapter localSimpleCursorAdapter = new SimpleCursorAdapter(getApplicationContext(), 2130903044, localCursor, arrayOfString2, arrayOfInt);
        localSimpleCursorAdapter.setViewBinder(new AudioListViewBinder());
        ListView localListView = (ListView)findViewById(2131099667);
        localListView.setAdapter(localSimpleCursorAdapter);
        Log.d("test", "start list()");
        localListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
          public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
          {
            switch (Recipe093.this.getIntent().getIntExtra("case1", 0))
            {
            default:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
            case 16:
            }
            while (true)
            {
              Intent localIntent = new Intent(Recipe093.this.getApplicationContext(), MainActivity.class);
              Recipe093.this.startActivity(localIntent);
              return;
              Recipe093.path[1] = localCursor.getString(1);
              SharedPreferences.Editor localEditor10 = Recipe093.this.getSharedPreferences("FileName", 3).edit();
              localEditor10.putString("userChoice", Recipe093.path[1]);
              localEditor10.commit();
              continue;
              Recipe093.path[2] = localCursor.getString(1);
              SharedPreferences.Editor localEditor9 = Recipe093.this.getSharedPreferences("FileName", 3).edit();
              localEditor9.putString("userChoice1", Recipe093.path[2]);
              localEditor9.commit();
            } 
          }
        });
        return;
      }
      Log.d("Recipe093", arrayOfString1[j]);
    }
  }

  private class AudioListViewBinder
    implements SimpleCursorAdapter.ViewBinder
  {
    private AudioListViewBinder()
    {
    }

    public boolean setViewValue(View paramView, Cursor paramCursor, int paramInt) {
        // TODO Auto-generated method stub
          int i = paramCursor.getColumnIndex("title");
          int j = paramCursor.getColumnIndex("artist");
          int k = paramCursor.getColumnIndex("duration");
          if ((paramInt == i) || (paramInt == j))
            ((TextView)paramView).setText(paramCursor.getString(paramInt));
        return false;
    }

  }
  }
4

3 回答 3

6

基本上,return;意思是“现在退出这个方法”。因此,return 语句之后的任何内容都不会运行。

您可以使用范围获得多个回报:

if(x == 1) {
    return;
    // Nothing will be called here on down in this scope, i.e. before `}`
}
x = 1;
return; 
// Nothing will be called here on down
于 2013-01-03T20:04:35.763 回答
1

If you can return; the code stops and returns to the first method. It will not run any code after this line. The same story with break;

于 2013-01-03T20:06:06.527 回答
1

正如山姆所说(你应该接受他的回答),return意味着现在退出。

但是,return对于返回 void 的方法是可选的,因为该方法的右大括号也将返回:

这些是等价的:

void someMethod(){
    doSomeStuff();
    return;
}

void someMethod(){
    doSomeStuff();
}

return除非您想提前返回,否则在 void 方法中使用被认为是不好的做法,但是任何显式返回都必须是有条件的,即switchorif语句的一部分。如果它们不是有条件的,则编译器知道该方法将始终在该点退出,并且它之后的任何代码都无法执行,因此您会看到编译器错误。

对于大多数 Java 程序员来说,第一个示例看起来是错误的。

PS。 while(true)太可怕了,但是,如果您想尽早退出该循环,则应使用breakwhich 将执行转移到 while 循环结束后的语句,而不是return.

于 2013-01-03T22:11:01.430 回答