我有一个包含几个复选框的对话框。创建对话框时,应根据指定的布尔变量的状态将框初始化为选中/取消选中。变量检查传递正常,但是当我使用CheckBox.setChecked(true / false)时,它会崩溃应用程序。
经过一些研究和玩耍后,我注意到我用来指向复选框的Widget变量没有初始化,但我不明白为什么。我用相应的id连接它,但它似乎没有坚持。甚至单步执行代码,在我的findViewbyId()调用变量后立即显示为null。我不知道为什么。
(虽然有5个复选框,但我将只显示一个代码,因为它们在功能上完全相同)
MatrixActionActivity.java
public class MatrixActionActivity extends AppCompatActivity {... private CheckBox mHotSimCheck, mPublicCheck, mNoiseCheck, mDiffGridCheck, mSilentCheck;...@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_actions); mHotSimCheck = (CheckBox)findViewById(R.id.checkbox_hotsim); // <-- mHotSimCheck registers as null even after this statement //This button opens preferences dialog mPrefsButt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MatrixActionActivity.this); LayoutInflater inflater = MatrixActionActivity.this.getLayoutInflater(); final View dialogView = inflater.inflate(R.layout.dialog_matrix_action_modifiers, null); dialogBuilder.setView(dialogView); final MyDeck deck = MyDeck.getInstance(); if(deck.isDiveDPActive) { Toast.makeText(MatrixActionActivity.this, "deck.isDiveDPActive = true.", Toast.LENGTH_SHORT).show(); // <-- shows fine when next statement is removed mHotSimCheck.setChecked(true); // <-- Causes crash }else{ mHotSimCheck.setChecked(false); }
慕容708150
侃侃尔雅
相关分类