0

私はこの一日中プログラミングをしていますが、帰国後は到達不能に陥っています。で

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

returnを削除すると、続行後に到達不能コードが表示されます。到達不能コードを取得するのはなぜですか?誰かが私を助けてくれることを願っています。ありがとう。これが私のコードです:

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

可能であればreturn;、コードは停止して最初のメソッドに戻ります。この行の後にコードは実行されません。と同じ話break;

于 2013-01-03T20:06:06.527 に答える
1

サムが言ったように(あなたは彼の答えを受け入れるべきです)、return今すぐ終了することを意味します。

ただし、returnvoidを返すメソッドの場合、メソッドの閉じ中括弧も次を返すため、オプションです。

これらは同等です:

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

void someMethod(){
    doSomeStuff();
}

return早期に戻りたい場合を除いて、voidメソッドで使用することは悪い習慣と見なされますが、明示的な戻りは条件付き、つまりswitchorifステートメントの一部である必要があります。それらが条件付きでない場合、コンパイラは、メソッドが常にその時点で終了し、その後のコードは実行できない可能性があることを認識しているため、コンパイラエラーが発生します。

ほとんどのJavaコーダーにとって、最初の例は間違っているように見えます。

PS。 while(true)恐ろしいですが、そのループから早く抜け出したい場合はbreak、whileループの終了後に、実行をステートメントに転送するを使用する必要がありますreturn

于 2013-01-03T22:11:01.430 に答える