2013-08-01 36 views
1

開關語句修復: switch語句僅返回最後一種情況,即情況4「#0R0dfdf0FF」。我如何解決這個問題,使文本視圖顯示在對話框中單擊的那個? 我是一個總新手,所以是的幫助真的很感激。開關語句僅返回最後一種情況

public class NoteEdit extends Activity { 
public EditText mTitleText; 
public EditText mBodyText; 
public EditText mColor; 
private NotesDbAdapter mDbHelper; 
private static final int DIALOG_ALERT = 10; 
Long mRowId; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    mDbHelper = new NotesDbAdapter(this); 
    mDbHelper.open(); 
    setContentView(R.layout.note_edit); 
    setTitle(R.string.done); 
    mTitleText = (EditText) findViewById(R.id.editTitle); 
    mBodyText = (EditText) findViewById(R.id.editNote); 
    mColor = (EditText) findViewById(R.id.editColor); 
    mRowId = (savedInstanceState == null) ? null : 
     (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID); 
    if (mRowId == null) { 
     Bundle extras = getIntent().getExtras(); 
     mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) 
           : null; 
    } 
    populateFields(); 
    setupActionBar(); 
} 
private void setupActionBar() { 

    getActionBar().setDisplayHomeAsUpEnabled(true); 

} 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     // This ID represents the Home or Up button. In the case of this 
     // activity, the Up button is shown. Use NavUtils to allow users 
     // to navigate up one level in the application structure. For 
     // more details, see the Navigation pattern on Android Design: 
     // 
     // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
     // 
     setResult(RESULT_OK); 
     finish(); 


    } 
    return super.onOptionsItemSelected(item); 
} 
private void populateFields() { 
    if (mRowId != null) { 
     Cursor note = mDbHelper.fetchNote(mRowId); 
     startManagingCursor(note); 
     mTitleText.setText(note.getString(
        note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); 
     mBodyText.setText(note.getString(
       note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))); 
     mColor.setText(note.getString(
       note.getColumnIndexOrThrow(NotesDbAdapter.KEY_COLOR))); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    saveState(); 
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId); 
} 
@Override 
protected void onPause() { 
    super.onPause(); 
    saveState(); 
} 
@Override 
protected void onResume() { 
    super.onResume(); 
    populateFields(); 
} 
private void saveState() { 
    String title = mTitleText.getText().toString(); 
    String body = mBodyText.getText().toString(); 
    String color = mColor.getText().toString(); 

    if (mRowId == null) { 
     long id = mDbHelper.createNote(title, body, color); 
     if (id > 0) { 
      mRowId = id; 
     } 
    } else { 
     mDbHelper.updateNote(mRowId, title, body, color); 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onMenuItemSelected(int featureId, MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.add: 
     showDialog(DIALOG_ALERT); 
     return true; 
    } 

    return super.onMenuItemSelected(featureId, item); 
} 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_ALERT: 
    // Create out AlterDialog 
    android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    final String[] colors = {"Blue", "Green", "Yellow", "Red", "Purple"}; 
    builder.setTitle(R.string.body); 
    builder.setItems(colors, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     // The 'which' argument contains the index position 
     // of the selected item 
      switch (which){ 
      case 0: 
       mColor.setText("#000000"); 
      case 1: 
       mColor.setText("#0000FF"); 
      case 2: 
       mColor.setText("#0R00FF"); 
      case 3: 
       mColor.setText("#0R00dsdFF"); 
      case 4: 
       mColor.setText("#0R0dfdf0FF"); 
      default: 
       break; 
      } 
    } }); 
    AlertDialog dialog = builder.create(); 
    dialog.show(); 
    } 
    return super.onCreateDialog(id); 
} 

}

+7

我沒有看到代碼就猜到了答案。每個'case'後面都缺少'break'。 –

+0

是的,但我會得到回答它的答覆點:P(當然,它肯定會被刪除..) – MightyPork

+0

@MightyPork。 Nope不會被刪除。這是一個合理的問題。 –

回答

7

您在開關分支的結束失蹤break;

+0

謝謝!傻我。 –

4

Fall Through。

您必須添加break

case 0: 
      mColor.setText("#000000"); 
      break; 

You can find that in docs

斷裂語句是必要的,因爲沒有它們,在開關塊語句落空:匹配殼體標籤之後在順序執行的所有陳述,而不管隨後的情況下標籤的表達的,直到遇到break語句。

1

當你沒有一個return你需要一個break否則會導致告吹

0

您需要休息;畢竟,除了最後一個案件,否則它會逐案倒閉

switch (which){ 
     case 0: 
      mColor.setText("#000000"); 
      break;   
     case 1: 
      mColor.setText("#0000FF"); 
      break;   
     case 2: 
      mColor.setText("#0R00FF"); 
      break; 
     case 3: 
      mColor.setText("#0R00dsdFF"); 
      break; 
     case 4: 
      mColor.setText("#0R0dfdf0FF"); 
     default: 
      break; 
     } 
相關問題