Search code examples
androidtextviewcharacter-arrays

How to set a Character array as text for TextView in Android?


    userString=(EditText)findViewById(R.id.letter);
    checkButton=(Button)findViewById(R.id.go);
    text=(TextView)findViewById(R.id.display);
    checkButton.setOnClickListener(new View.OnClickListener(){
    @SuppressWarnings("null")
public void onClick(View v){
    char answer[]=null;int i;
    String userEntry=userString.getText().toString();
    for(i=0;i<userEntry.length();i++)
    {
    answer[i]='_';
    }
    text.setText(answer, 0, i);
    }
    }});

When I run the above code it says"The Application has stopped unexpectedly.Please Try again".How do i solve this? Thanks


Solution

  • you are trying to place items (characters) into an array that has not been allocated. you need something like

    char answer[] = new char[size];
    

    at some point prior to placing characters into it.