Adham
Adham

Reputation: 64844

Can't cast from View to EditText

I have EditText that is added from java NOT xml resource . when I want access the EditText I get this error :

01-13 13:17:22.084: E/AndroidRuntime(21787): java.lang.ClassCastException: android.view.View cannot be cast to android.widget.EditText

here where I am trying to access it :

private List<Double> getFieldsData() {
    List<Double> data = new ArrayList<Double>();
    for (int i = 0; i < lytContainer.getChildCount(); i++) {
        LinearLayout lyt = (LinearLayout) lytContainer.getChildAt(i);

        View txt1 = lyt.getChildAt(0);
        View txt2 = lyt.getChildAt(1);

        valid = Util.checkValidation(getApplicationContext(),
                ((EditText) txt1), ((EditText) txt2));

        Double val1 = Double.parseDouble(((EditText) txt1).getText()
                .toString());
        Double val2 = Double.parseDouble(((EditText) txt2).getText()
                .toString());

        data.add(val1);
        data.add(val2);
    }
    return data;
}

here where I add the EditText :

private void createFields(int count) {
    for (int i = 0; i < count; i++) {

        LinearLayout lyt = new LinearLayout(getApplicationContext());
        LinearLayout.LayoutParams p = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lyt.setLayoutParams(p);

        LinearLayout.LayoutParams p2 = new LayoutParams(0,
                LayoutParams.WRAP_CONTENT, 1);

        LinearLayout.LayoutParams pv = new LayoutParams(0, 0, 1);
        TextView txt1 = (EditText) getLayoutInflater().inflate(
                R.layout.my_edittext, null);
        TextView txt2 = (EditText) getLayoutInflater().inflate(
                R.layout.my_edittext, null);

        txt1.setInputType(InputType.TYPE_CLASS_NUMBER);
        txt2.setInputType(InputType.TYPE_CLASS_NUMBER);
        txt1.setLayoutParams(p2);
        txt2.setLayoutParams(p2);

        lyt.setPadding(20, 4, 20, 4);

        txt1.setTextColor(Color.BLACK);
        txt2.setTextColor(Color.BLACK);

        lyt.addView(txt1);

        View v = new View(getApplicationContext());
        v.setLayoutParams(pv);
        lyt.addView(v);
        lyt.addView(txt2);

        lytContainer.addView(lyt);

    }
}

Upvotes: 1

Views: 1990

Answers (3)

Mark
Mark

Reputation: 5566

Your code looks strange but from what I see the error is here:

 lyt.addView(txt1); // view 0
 View v = new View(getApplicationContext());
 v.setLayoutParams(pv);
 lyt.addView(v); // view 1
 lyt.addView(txt2); // view 2

Your child(1) is not EditText but View. Use child(0) and child(2) instead.

Btw. Why are you doing this?

TextView txt1 = (EditText) getLayoutInflater().inflate(R.layout.my_edittext, null);

Why are you using TextView here? Do you also need this strange View v added to your layout, what is it for?

Upvotes: 1

Bipin Bhandari
Bipin Bhandari

Reputation: 2692

Change

TextView txt1 = (EditText) getLayoutInflater().inflate(
            R.layout.my_edittext, null);
    TextView txt2 = (EditText) getLayoutInflater().inflate(
            R.layout.my_edittext, null);

to

EditText txt1 = (EditText) getLayoutInflater().inflate(
            R.layout.my_edittext, null);
    EditText txt2 = (EditText) getLayoutInflater().inflate(
            R.layout.my_edittext, null);

Upvotes: 0

Sanket990
Sanket990

Reputation: 665

In your createField Method you are wrong casting in below line

TextView txt1 = (EditText) getLayoutInflater().inflate(
            R.layout.my_edittext, null);
    TextView txt2 = (EditText) getLayoutInflater().inflate(
            R.layout.my_edittext, null);

Change TextView to EditText(when You Inflating the view) //Changes in createFields Method

 EditTexttxt1 = (EditText) getLayoutInflater().inflate(
                R.layout.my_edittext, null);
        EditTexttxt2 = (EditText) getLayoutInflater().inflate(
                R.layout.my_edittext, null);

Upvotes: 2

Related Questions