black.empiricist
black.empiricist

Reputation: 47

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

I have a Checkbox that sets a String's value when the Checkbox is checked or not checked. But my String is returning a null value. I have declared the string inside the Public class of the Activity. Why is it returning null?

I want the strings to be inserted to a database FYI.

     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();
        }

    }

}

Upvotes: 0

Views: 1157

Answers (2)

Athena
Athena

Reputation: 476

Its because you haven't initialized LabText and LecText. The Toast messages are part of your activity's lifecycle. Meaning, they will be executed whether you click the checkboxes or not. It just so happens that at their time of execution, LabText and LecText are null.

Solution is to initialize them property:

String LabText = "", LecText= "";

And if you want a Toast to show when the checkbox is clicked, place:

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

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

inside your OnCheckedChangeListeners.

Edited code:

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();

Upvotes: 1

Shadik Khan
Shadik Khan

Reputation: 1215

complete working code as per your expectation.. As I understand your question.

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();
    }    
}

Let me know if you still; face the problem

Upvotes: 0

Related Questions