Search code examples
if-statementcontains

If else not working in android inside for and while loop


public void LoadRoutine() {
        TableRow tbrow0 = new TableRow(getActivity());

        //String[] mStrings = new String[9];
        tbrow0.setBackgroundColor(Color.parseColor("#FFFFFF"));
        tbrow0.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        for (String c : TimeSlotSummer) {
            TextView tv0 = new TextView(getActivity());
            tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            tv0.setGravity(Gravity.CENTER);
            tv0.setTextSize(12);
            tv0.setHeight(40);
            tv0.setWidth(76);
            tv0.setBackgroundColor(Color.parseColor("#FFFFFF"));
            tv0.setTextColor(Color.parseColor("#000000"));
            tv0.setPadding(1, 1, 1, 1);
            tv0.setText(c);
            tv0.setBackgroundColor(R.id.tableRowid);
            tbrow0.addView(tv0);
        }
        tableLayout.addView(tbrow0);

        String dept = GlobalClass.userDepartment;

        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity());
        databaseAccess.Open();
        String faccode = GlobalClass.faculty_code;
        Cursor cRoutine = databaseAccess.getRoutine("Sunday",dept);

        if (cRoutine.getCount() == 0) {
            Toast.makeText(getActivity(),"No Data in Table",Toast.LENGTH_LONG).show();
        }
        else{
            while (cRoutine.moveToNext())
            {
                String[] mStrings = new String[10];

                mStrings[0] = cRoutine.getString(2);
                mStrings[1] = cRoutine.getString(4);
                mStrings[2] = cRoutine.getString(5);
                mStrings[3] = cRoutine.getString(6);
                mStrings[4] = cRoutine.getString(7);
                mStrings[5] = cRoutine.getString(8);
                mStrings[6] = cRoutine.getString(9);
                mStrings[7] = cRoutine.getString(10);
                mStrings[8] = cRoutine.getString(11);


                TableRow tbrow1 = new TableRow(getActivity());
                tbrow1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                tbrow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

                for (String cls:mStrings) {
                    TextView tv1 = new TextView(getActivity());
                    tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                    tv1.setGravity(Gravity.CENTER);
                    tv1.setTextSize(12);
                    tv1.setWidth(75);
                    tv1.setHeight(37);

                    tv1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                    tv1.setPadding(1, 1, 1, 1);
                    tv1.setBackgroundColor(R.id.tableRowid);
                    tv1.setText(cls);
                    tv1.setTextColor(Color.parseColor("#FF0000"));
                    if(cls.contains(faccode))
                        tv1.setTextColor(Color.parseColor("#000000"));
                    else
                        tv1.setTextColor(Color.parseColor("#FF0000"));
                    tbrow1.addView(tv1);
                }
                tableLayout.addView(tbrow1);
            }
        }
    }

Inside the last for loop, without if-else, it works properly, but with if-else it is not working, that is apps shut down and mobile restart again.

Any one help me, I want to check some substring, then text color will change, otherwise color normal.


Solution

  • It would help if you posted a stack trace but if the crash occurs here:

                if(cls.contains(faccode))
                    tv1.setTextColor(Color.parseColor("#000000"));
                else
                    tv1.setTextColor(Color.parseColor("#FF0000"));
    

    The only explanation is that you get a NullPointerException on cls because tv1 is obviously not null if it did not crash before.

    Use this code instead:

                if(cls != null && cls.contains(faccode))
                    tv1.setTextColor(Color.parseColor("#000000"));
                else
                    tv1.setTextColor(Color.parseColor("#FF0000"));