使用 OnCheckedChangedListener 在复选框中设置字符串值 returns Android 中的空值

Setting a string value in a Checkbox using OnCheckedChangedListener returns a null value in Android

我有一个 Checkbox,它在选中或不选中 Checkbox 时设置 String 的值。但是我的 String 返回了一个空值。我已经在 ActivityPublic class 内声明了字符串。为什么返回 null?

我希望将字符串插入数据库以供参考。

     public class ClassFormCreate extends Activity
{
    SQLiteDatabase db;
    String Name, Desc;
    String LabText = null;
    String LecText = null;
    CheckBox Lab, Lec;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.classes_addform);
        db=openOrCreateDatabase("ClassRecords",MODE_WORLD_READABLE, null);

    }

    public void AddClassFunction (View view)
    {

        EditText NameText          = (EditText)findViewById(R.id.NameText);
        EditText DescText          = (EditText)findViewById(R.id.DescriptionText);
        Lab                        = (CheckBox)findViewById(R.id.LabCheck);
        Lec                        = (CheckBox)findViewById(R.id.RoomCheck);


        Name                 = NameText.getText().toString();
        Desc                 = DescText.getText().toString();

        Lab.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                if(b)
                {
                    LabText = "withlab";

                }
                else
                {
                    LabText = "without";
                }
            }
        });

        Lec.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                if (b)
                {
                    LecText = "corner";
                }

                else
                {
                    LecText = "notcorner";
                }
            }
        });

        Toast toast2 = Toast.makeText(getApplicationContext(), LecText, Toast.LENGTH_SHORT);
        toast2.show();


        if (Name.equals(""))
        {
            Toast toast = Toast.makeText(getApplicationContext(), "Please insert a Class Name", Toast.LENGTH_SHORT);
            toast.show();
        }

        else if (Desc.equals(""))
        {
            Toast toast = Toast.makeText(getApplicationContext(), "Please insert a Description", Toast.LENGTH_SHORT);
            toast.show();
        }
        else
        {

        db.execSQL("INSERT INTO MasterClasslist (Name, Desc, SeatPlan, Lab) VALUES ('" + Name + "','" + Desc + "', '" + LecText +"' , '" + LabText + "');");

        Cursor c2 = db.rawQuery("SELECT * FROM MasterClasslist", null);
        c2.moveToLast();

        final String IDcontainer = c2.getString(c2.getColumnIndex("ClassID"));



            db.execSQL("CREATE TABLE '" + IDcontainer + "'(StudentID INTEGER NOT NULL UNIQUE, FirstName TEXT, LastName TEXT, pQuiz INTEGER DEFAULT '0', pAttend INTEGER DEFAULT '0', pRecite INTEGER DEFAULT '0'," +
                    "pProject INTEGER DEFAULT '0', pHomework INTEGER DEFAULT '0', pOthers INTEGER DEFAULT '0', pExam INTEGER DEFAULT '0'," +
                    "mQuiz INTEGER DEFAULT '0', mAttend INTEGER DEFAULT '0', mRecite INTEGER DEFAULT '0'," +
                    "mProject INTEGER DEFAULT '0', mHomework INTEGER DEFAULT '0', mOthers INTEGER DEFAULT '0', mExam INTEGER DEFAULT '0'," +
                    "fQuiz INTEGER DEFAULT '0', fAttend INTEGER DEFAULT '0', fRecite INTEGER DEFAULT '0'," +
                    "fProject INTEGER DEFAULT '0', fHomework INTEGER DEFAULT '0', fOthers INTEGER DEFAULT '0', fExam INTEGER DEFAULT '0'," +
                    "pGrade INTEGER DEFAULT '0', mGrade INTEGER DEFAULT '0', fGrade INTEGER DEFAULT '0', semGrade INTEGER DEFAULT '0' );");

            Toast toast = Toast.makeText(getApplicationContext(), "Class Added", Toast.LENGTH_SHORT);
            toast.show();

            Intent ClassesMasterListIntent = new Intent(getApplicationContext(), ClassesMasterList.class);

            startActivity(ClassesMasterListIntent);

            overridePendingTransition(R.anim.transition, R.anim.right2left);

            finish();
        }

    }

}

这是因为您还没有初始化LabText 和LecText。 Toast 消息是您 activity 生命周期的一部分。意思是,无论您是否单击复选框,它们都会被执行。碰巧在执行时,LabText 和 LecText 为空。

解决方案是初始化它们属性:

String LabText = "", LecText= "";

如果您希望在单击复选框时显示 Toast,请放置:

Toast.makeText(this,LabText, Toast.LENGTH_SHORT).show();

Toast.makeText(this,LecText, Toast.LENGTH_SHORT).show();

在你的 OnCheckedChangeListeners.

里面

编辑代码:

Lab = (CheckBox)findViewById(R.id.LabCheck);
Lec = (CheckBox)findViewById(R.id.RoomCheck);

if(Lab.isChecked()) {
    LabText = "withlab";    
} else {
    LabText = "without";
}

if (Lec.isChecked()) {
    LecText = "corner";
} else {
    LecText = "notcorner";
}

Toast.makeText(this,LabText, Toast.LENGTH_SHORT).show();

Toast.makeText(this,LecText, Toast.LENGTH_SHORT).show();

按照您的期望完成工作代码。据我了解您的问题。

public class MainActivity extends ActionBarActivity {

    CheckBox Lab,Lec;
    String LabText="";
    String LecText="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Lab = (CheckBox)findViewById(R.id.LabCheck);
        Lec = (CheckBox)findViewById(R.id.RoomCheck);

        if(Lab.isChecked()) {
            LabText = "withlab";    
        } else {
            LabText = "without";
        }

        if (Lec.isChecked()) {
            LecText = "corner";
        } else {
            LecText = "notcorner";
        }

        Toast toast1 = Toast.makeText(getApplicationContext(), 
                            LabText, Toast.LENGTH_SHORT);
        toast1.show();

        Toast toast2 = Toast.makeText(getApplicationContext(), 
                            LecText, Toast.LENGTH_SHORT);
        toast2.show();
    }    
}

如果你还在,请告诉我;面对问题