-2

setText is shown in red and i don't undrestand why

public class MainActivity extends AppCompatActivity {

    TextView v = (TextView)findViewById(R.id.textInput);
    Button b = (Button)findViewById(R.id.btn12);
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b.setOnClickListener((new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                v.setText("aaa");// why setText not found
            }
        }));


    }

}

thx

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
xXxlazharxXx
  • 433
  • 4
  • 8

3 Answers3

1

You are getting the error because the setText() method is not available in View

and you trying to access a View.setText("aaa"); inside onClick () method

Make below changes in your code

  1. Do findViewById() inside onCreate() method
  2. Rename your TextView variable

Try this

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button b ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        
        textView = (TextView)findViewById(R.id.textInput);
        b = (Button)findViewById(R.id.btn12)
        b.setOnClickListener((new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                textView.setText("aaa");// why setText not found
            }
        }));


    }

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

setText is not a method of view you need to case to be an EditText as long as the view will always be an EditText:

public class MainActivity extends AppCompatActivity {

TextView v = (TextView)findViewById(R.id.textInput);
Button b = (Button)findViewById(R.id.btn12);
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b.setOnClickListener((new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            ((EditText)v).setText("aaa");// why setText not found
        }
    }));


}

}

Boardy
  • 35,417
  • 104
  • 256
  • 447
0

Move these methods:

TextView v = (TextView)findViewById(R.id.textInput);
Button b = (Button)findViewById(R.id.btn12);

You can use:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView v = (TextView)findViewById(R.id.textInput);
    Button b = (Button)findViewById(R.id.btn12);
    b.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            v.setText("aaa");// why setText not found
        }
    });

or you can use:

public class MainActivity extends AppCompatActivity {

    private TextView v;
    private Button b;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        v = (TextView)findViewById(R.id.textInput);
        b = (Button)findViewById(R.id.btn12);
        b.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View view) {
            v.setText("aaa");// why setText not found
           }
        });

    }

}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841